1 of 39

JavaScript Primer

CSCI 344: Advanced Web Technologies

Fall 2024

2 of 39

Announcements

  1. HW2 – Due this Friday at midnight (9/20)
  2. Tutorial 5 – forthcoming (Practice with JavaScript Functions and Event Handlers)
  3. Quiz 1 next Monday (9/23) during class: Web Architecture, HTML, CSS, and Design
    • Study guide posted
    • You may bring a 1-page cheatsheet

3 of 39

Before starting with JavaScript…

Some HW2 hints

4 of 39

5 of 39

Fixed (they don’t move when the page scrolls)

TIP: Use position: fixed for these elements

6 of 39

Note that the “Suggestions for You” panel is hidden on mobile

TIP: Use display: none for this element inside of the media query

7 of 39

Download Today’s Lecture Files

  1. Open your entire csci344 folder in VS Code
  2. Navigate to the lecture08/hw02-help
  3. Let’s work on a layout: 01-starter-code. TODOs:
    1. Get layout working on Desktop
    2. Hide aside on Mobile layouts
  4. Using Font Awesome (icon library): 02-icons-font-awesome

8 of 39

JavaScript Time!

9 of 39

JavaScript Readings & References

  • Programming Review with JavaScript (Course Website)
    • You will be expected to know basic programming concepts (data, expressions & statements, control, objects & classes)
    • We will be learning how these concepts work in JavaScript
    • All of the practice exercises will be fair game for Quiz #2.
  • JavaScript Cheatsheet (Course Website)
    • Created to help you with common JavaScript tasks, including DOM manipulation
  • Will assign additional JavaScript readings as needed (many from Digital Ocean)

10 of 39

Outline

  1. Data Types
  2. Variables
  3. Conditionals; logical and comparison operators
  4. Functions
  5. Loops and iteration
  6. Practice

11 of 39

Outline

  1. Data Types
  2. Variables
  3. Conditionals; logical and comparison operators
  4. Functions
  5. Loops and iteration
  6. Practice

12 of 39

JavaScript Data Types

There are 8 basic data types in JavaScript:

12

number

For numbers of any kind: integer or floating-point

1.4, 33, 99999999

bigint

For storing very large integers that can’t be represented by the number datatype

BigInt("123456789012345678901234567890")

string

For strings (text). A string may have one or more characters - no single character type

‘hello world!’

boolean

for true/false.

true, false

null

for unknown values – has a single value null

null

undefined

for unassigned values – has a single value undefined

undefined

object

for more complex data structures.

{ name: 'ralph', species: 'dog' }

symbol

for unique identifiers

A way to generate unique identifiers

13 of 39

Figuring out what data type you have

typeof 'hello world!';

typeof true;

typeof false;

typeof null;

typeof undefined;

typeof 23.4;

typeof [1, 3, 4, 6];

typeof { name: 'Lester', species: 'dog', age: 15};�

See 01_datatypes.js

13

14 of 39

Functions that convert between data types

// String(), Number(), Boolean()

console.log(123, typeof 123, String(123), typeof String(123));

console.log('123', typeof '123', Number('123'), typeof Number('123'));

console.log('true', typeof 'true', Boolean('true'), typeof Boolean('true'));

See 01_datatypes.js

14

15 of 39

Outline

  1. Data Types
  2. Variables
  3. Conditionals; logical and comparison operators
  4. Functions
  5. Loops and iteration
  6. Practice

16 of 39

Declaration keywords

In JavaScript, there are three keywords for declaring variables: let, const, and var

  1. let – Means that the variable may be reassigned. It also signals that the variable will be used only in the block it's defined in (we’ll talk about this more when we get to functions).
  2. const – Means that the variable won't be reassigned. It’s immutable. Used in cases where you declare and assign once, but don’t change it afterwards.
  3. varBest avoid nowadays. Old way to do things prior to ES6.�“The weakest signal available” — “The variable may or may not be reassigned, and the variable may or may not be used for an entire function.”

16

17 of 39

Naming Variables: Case Sensitivity

JavaScript is case-sensitive.

let a = 3;

let A = 33;

console.log(a);

console.log(A);

17

18 of 39

Naming Variables: Conventions

Although JavaScript doesn’t care what you name your variables (beyond the rules specified in the previous slide), there are some conventions that most people follow:�

  1. “camelCase” for functions and variable names
    1. firstName = 'Beyonce';
    2. lastName = 'Knowles';
  2. Hyphens for file names: hello-world.js
  3. Capital letters for classes

Tl;dr: same general conventions as Java

18

19 of 39

See 02_variables.js

19

20 of 39

Outline

  1. Data Types
  2. Variables
  3. Conditionals; logical and comparison operators
  4. Functions
  5. Loops and iteration
  6. Practice

21 of 39

If / Else Statement

21

if (a === 5) {

console.log('a is 5');

} else if (a > 5) {

console.log('a greater than 5');

} else {

console.log('a is less than 5');

}

22 of 39

Comparison Operators

= Assignment

== Equality

=== Strict Equality (both values and data types match)

!=, !== Not Equal

>, >= Greater than; greater than or equal to

<, <= Less than; Less than or equal to

See 03_operators.js

22

23 of 39

Operator Quiz

const num1 = "50";

const num2 = 50;

Will this statement evaluate to true or false?

num1 == num2;

24 of 39

Operator Quiz

const num1 = "50";

const num2 = 50;

Will this statement evaluate to true or false?

num1 === num2;

25 of 39

I. OPERATOR QUIZ

const num1 = "50";

const num2 = 50;

What will be stored in the “result” variable?

const result = num1 + num2;

26 of 39

Logical Operators

&& AND

|| OR

! NOT

26

27 of 39

Outline

  • Data Types
  • Variables
  • Conditionals; logical and comparison operators
  • Functions
  • Loops and iteration
  • Practice

28 of 39

Functions

  1. There are a few different ways to create a function in JavaScript, 2 of which we’ll be discussing in this class (we’ll be ignoring function expressions and function constructors, at least for the time being).
  2. We will be focusing on:

Function Declarations

function add(a, b) {

return a + b;

}

Arrow Functions (new in ES6)

const add = (a, b) => a + b;

29 of 39

Function Declarations

Function declaration that add two numbers:�

function add(a, b) {

return a + b;

}

Note that no return or parameter types are explicitly defined. That said, TypeScript extends JavaScript by adding types...and is then compiled into JavaScript.

30 of 39

Arrow Functions

  1. Newer syntax, introduced in ES6.
  2. In-depth explanation of the differences between arrow functions and function declarations on MDN website.
  3. There are some limitations of arrow functions in the context of object-oriented programming
    1. It doesn’t honor the “this” keyword
    2. You can’t use arrow functions to define methods inside a class definition
    3. You have to define an arrow function before you invoke it

31 of 39

Arrow Functions

// verbose syntax:�const add_two_nums = (num1, num2) => {� return num1 + num2;�};

// condensed syntax (if statement only 1 line):�const add_two_nums = (num1, num2) => num1 + num2;

// condensed syntax, (if only 1 argument; statement only 1 line:�const square_number = num1 => num1 ** 2;

32 of 39

Demo: 04_functions.js

33 of 39

Outline

  • Data Types
  • Variables
  • Conditionals; logical and comparison operators
  • Functions
  • Loops and iteration
  • Practice

34 of 39

Iteration and Looping

  1. “for” and “while” loops (the classics)
  2. for...of loop (to access each item in a list, sequentially)
  3. Built-in list functions (forEach, map, filter, and reduce) – Wednesday

34

05_loops_and_iteration.js

35 of 39

1. For and While loops (“Classic” Loop Structures)

// while loop�let i = 0;�while (i < 5) {� console.log('The counter is:', i);� i++;�}

// for loop�for (let i = 0; i < 5; i++) {� console.log('The counter is:', i);�}

35

05_loops_and_iteration.js

36 of 39

2. For .. Of Syntax

36

for (const item of array) {

// do something� // with item

...

}

For item in array�assigns each item in the array to a variable name of your choice

BLOCK�If the statements that will repeat for each item in the array

37 of 39

2. For .. Of Syntax

const fruit = ['apple', 'orange', 'banana', 'mango', 'peach'];

// for…of loop

for (const item of fruit) {

console.log('The item is:', item);

}

// “traditional” for loop�for (let i = 0; i < fruit.length; i++) {� console.log('The item is:', fruit[i]);�}

38 of 39

Demo: 05_loops_and_iteration.js

39 of 39

Practice

  1. Navigate to the Programming Review page
  2. Scroll down to Section #6: Exercises
  3. Try completing the following using node.js.
    1. 6.1.1 (sum of numbers) – Note that you will have to install prompt-sync via in order to complete
    2. 6.2.1. (which decade)
    3. On your own: see if you can complete the rest!