1 of 35

JavaScript Primer

CSCI 344: Advanced Web Technologies

Spring 2025

2 of 35

Announcements

  1. HW2 – Due this Friday at midnight (2/14)
  2. Quiz 1 this Friday (9/23) during class: Web Architecture, HTML, CSS, and Design
    • Study guide posted
    • You may bring a 1-page cheatsheet
  3. Tutorial 3 – Due Wednesday (2/12) – video walkthrough posted
  4. Tutorial 4 – during class on Wednesday (programming practice)

3 of 35

Some HW2 hints

  • Check out the sample code / HW2 getting started walkthrough
  • Download today’s activity files
    • Font Awesome demo
    • Accessibility demo

4 of 35

Intro to JavaScript

5 of 35

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)

6 of 35

Outline

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

7 of 35

Outline

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

8 of 35

JavaScript Data Types

There are 8 basic data types in JavaScript:

8

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

9 of 35

Which data type? Use the typeof operator

typeof 'hello world!';

typeof true;

typeof null;

typeof undefined;

typeof 23.4;

typeof [1, 3, 4, 6];

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

�Demo: see 01-datatypes.js

9

10 of 35

Functions that convert between data types

JavaScript has built-in functions to convert between types:�

  • String()
  • Number()
  • Boolean()

// Examples

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

10

11 of 35

Outline

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

12 of 35

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.”

12

13 of 35

Naming Variables: Case Sensitivity

JavaScript is case-sensitive.

let a = 3;

let A = 33;

console.log(a);

console.log(A);

13

14 of 35

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

Same general conventions as Java

14

15 of 35

See 02-variables.js

15

16 of 35

Outline

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

17 of 35

If / Else Statement

17

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');

}

18 of 35

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

18

19 of 35

Operator Quiz

const num1 = "50";

const num2 = 50;

Will this statement evaluate to true or false?

num1 == num2;

20 of 35

Operator Quiz

const num1 = "50";

const num2 = 50;

Will this statement evaluate to true or false?

num1 === num2;

21 of 35

I. OPERATOR QUIZ

const num1 = "50";

const num2 = 50;

What will be stored in the “result” variable?

const result = num1 + num2;

22 of 35

Logical Operators

&& AND

|| OR

! NOT

22

23 of 35

Outline

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

24 of 35

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;

25 of 35

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.

26 of 35

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

27 of 35

Arrow Functions

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

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

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

28 of 35

Demo: 04-functions.js

29 of 35

Outline

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

30 of 35

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

30

05-loops-and-iteration.js

31 of 35

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);�}

31

05-loops-and-iteration.js

32 of 35

2. For .. Of Syntax

32

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

33 of 35

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]);�}

34 of 35

Demo: 05-loops-and-iteration.js

35 of 35

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!