WDD 131: Dynamic Web Fundamentals

W02 Learning Activity: JavaScript Constructs

Overview

Through the prerequisite programming courses, you have built a foundational understanding of common, programming constructs, including operators, expressions, decision structures, loops, and functions. This activity focuses on a few of the common control structures in programming.

Associated Course Learning Outcomes

Demonstrate proficiency with JavaScript language syntax.

Prepare

Operators

In programming, operators are special symbols (characters) that are used to perform operations on operands (variables and values) and to process expressions. Most operators are used to perform mathematical operations such as addition, subtraction, multiplication, etc. However, there are other operators that are used to perform other operations such as assignment, comparison, and logical operations.

Assignment Operators

Assignment operators are used to assign values to variables.

Operator
Description
Example
Same As
=
Assigns a value to a variable
x = y
x = y
+=
Adds a value to a variable
x += y
x = x + y
-=
Subtracts a value from a variable
x -= y
x = x - y
*=
Multiplies a variable by a value
x *= y
x = x * y
/=
Divides a variable by a value
x /= y
x = x / y
%=
Assigns a remainder to a variable
x %= y
x = x % y
**=
Exponentiates a variable by a value
x **= y
x = x ** y
&&=
Logical AND assignment
x &&= y
x = x && y
||=
Logical OR assignment
x ||= y
x = x || y

Example:

let x = 5;
let y = 10;
x += y; // x is now 15 and y is still 10

You can write JavaScript code directly in the browser's console to test it yourself. To do this in most browsers, right-click on the page, select "Inspect" or "Inspect Element," and then go to the "Console" tab. You can then type directly into the console and press Enter to execute it.

Video Demonstration: [ 20 seconds ]

Key Points:

Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations.

Operator
Description
Example
Same As
+
Addition
x + y
x + y
-
Subtraction
x - y
x - y
*
Multiplication
x * y
x * y
/
Division
x / y
x / y
%
Modulus (Remainder)
x % y
x % y
**
Exponentiation
x ** y
x ** y

Example:

let a = 7;
let b = 3;
let c = a + b; // c is now 10
let d = a * 100; // d is now 700
let e = a % b; // e is now 1

Key Points:

Comparison Operators

Comparison operators are used to compare two values and return a boolean value (true/false) based on the comparison.

Operator
Description
Example
Returns
==
Equal to
x == y
true if x is equal to y
===
Strict equal to
x === y
true if x is equal to y and they are of the same data type
!=
Not equal to
x != y
true if x is not equal to y
!==
Strict not equal to
x !== y
true if x is not equal to y or they are of different types
<
Less than
x < y
true if x is less than y
<=
Less than or equal to
x <= y
true if x is less than or equal to y

Example:

let x = 10;
let y = 5;
let z = x + y;
console.log(z == 15); // true
console.log(z === "15"); // false
console.log(x < y); // false

Key Points:

Logical Operators

Logical operators are used to combine multiple boolean expressions and return a boolean value based on the compounded logical operation.

Operator
Description
Example
Returns
&&
Logical AND
x && y
true if both x and y are true
||
Logical OR
x || y
true if either x or y is true
!
Logical NOT
!x
true if x is false; false if x is true

Example:

let a = true;
let b = false;
let c = a && b;
console.log(c); // false
let d = a || b;
console.log(d); // true
let e = !a;
console.log(e); // false

Key Points:


Expressions

An expression is a combination of values, variables, operators, and functions that are evaluated to produce a single value. Expressions are used to perform calculations, make decisions, and manipulate data in programming.

Types of Expressions

Decisions

Conditional structures are used to make decisions in programming. They allow the program to execute different blocks of code based on whether a specified condition is true or false. The most common conditional structures in JavaScript are the if statement, the else statement, and the else if statement.


Loops

Loops are used to repeat a block of code multiple times until a specified condition is met. The most common loop structures in JavaScript are the for loop, the while loop, and the forEach loop.

Activity Instructions

  1. Given the following variable declarations:
    const DAYS = 6;
    const LIMIT = 30;
    let studentReport = [11, 42, 33, 64, 29, 37, 44];
    Create the following programming snippets in the console of your web browser:
    • Write a for loop that will iterate through the studentReport array and print to the console the current array value if it is below 30.
    • Repeat the previous programming snippet by using a while loop.
    • Repeat the previous programming snippet by using a forEach loop.
    • Repeat the previous programming snippet by using a for...in loop.
    • Use any type of repetition (looping) statement to dynamically produce the day names (Monday, Tuesday, Wednesday, etc.) of the next number of DAYS from today's date.
    Check Your Understanding – Example Answers

    These answers are certainly not exhaustive.

    
          // for loop
          for (let i = 0; i < studentReport.length; i++) {
            if (studentReport[i] < LIMIT) {
              console.log(studentReport[i]);
            }
          }
        
          // while loop
          let i = 0;
          while (i < studentReport.length) {
            if (studentReport[i] < LIMIT) {
              console.log(studentReport[i]);
            }
            i++;
          }
        
          // forEach loop
          studentReport.forEach(function (item) {
            if (item < LIMIT) {
              console.log(item);
            }
          });
        
          // for...in loop
          for (let i in studentReport) {
            if (studentReport[i] < LIMIT) {
              console.log(studentReport[i]);
            }
          }

    Use this CodePen to guide you in a solution to the last question. This requires you to sift through the example code to find what is applicable.

  2. In this CodePen ⚙️ JavaScript – Get Future Days from Today, look for the loop in the JavaScript panel that generates future day names based on the current day. Fork the pen and modify the code to use a different type of loop than originally implemented (for, while, forEach, for...in).

Optional Resources