reading-notes

View on GitHub

Class 08

Contents

Javascript

Expressions and Operators

Assignment operators

An assignment operator assigns a value to its left operand based on the value of its right operand. The simple assignment operator is equal = which assigns the value of its right operand to its left operand. That is, x = f() is an assignment expression that assigns the value of f() to x.

MDN offers the following table for assignment operators.

Name Shorthand Operator Meaning
Assignment x = f() x = f()
Addition assignment x += f() x = x + f()
Subtraction assignment x -= f() x = x - f()
Multiplication assignment x *= f() x = x * f()
Division assignment x /= f() x = x / f()
Remainder assignment x %= f() x = x % f()
Exponentiation assignment x **= f() x = x ** f()
Left shift assignment x <<= f() x = x << f()
Right shift assignment x >>= f() x = x >> f()
Unsigned right shift assignment x >>>= f() x = x >>> f()
Bitwise AND assignment x &= f() x = x & f()
Bitwise XOR assignment x ^= f() x = x ^ f()
Bitwise OR assignment x &#124;= f() x = x &#124; f()
Logical AND assignment x &&= f() x && (x = f())
Logical OR assignment x &#124;&#124;= f() x &#124;&#124; (x = f())
Logical nullish assignment x ??= f() x ?? (x = f())

Comparison operators

A comparison operator compares its operands and returns a logical value based on whether the comparison is true. he operands can be numerical, string, logical, or object values. If two operands are of different types then Javascript will try and convert them to match them up. For example 2 == '2' returns true. Javascript recognises one as a string and the other as a number but converts them to match. There are two exceptions to this which is the strict equality === and strict inequality !== operators. These simply do not do any conversion at all.

MDN offers the following table for comparison operators.

Operator Description Examples returning true
Equal (==) Returns true if the operands are equal. 3 == var1,"3" == var1, 3 == '3'
Not equal (!=) Returns true if the operands are not equal. var1 != 4, var2 != "3"
Strict equal (===) Returns true if the operands are equal and of the same type. See also Object.is and sameness in JS. 3 === var1
Strict not equal (!==) Returns true if the operands are of the same type but not equal, or are of different type. var1 !== "3", 3 !== '3'
Greater than (>) Returns true if the left operand is greater than the right operand. var2 > var1, "12" > 2
Greater than or equal (>=) Returns true if the left operand is greater than or equal to the right operand. var2 >= var1, var1 >= 3
Less than (<) Returns true if the left operand is less than the right operand. var1 < var2, "2" < 12
Less than or equal (<=) Returns true if the left operand is less than or equal to the right operand. var1 <= var2, var2 <= 5

Loops and iteration

Loops offer a quick and easy way to do something repeatedly. They are used to repeat an action number of times without having to repeat the same line of code. Conditions typically return true or false. A loop will continue running until the defined condition returns false.

for statement

A for loop repeats until a specified condition evaluates to false.

MDN provides th below example.

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement

When a for loop executes, the following occurs:

  1. The initializing expression initialExpression, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity. This expression can also declare variables.
  2. The conditionExpression expression is evaluated. If the value of conditionExpression is true, the loop statements execute. Otherwise, the for loop terminates. (If the conditionExpression expression is omitted entirely, the condition is assumed to be true.)
  3. The statement executes. To execute multiple statements, use a block statement ({ }) to group those statements.
  4. If present, the update expression incrementExpression is executed.
  5. Control returns to Step 2.

while statement

A while statement executes its statements as long as a specified condition evaluates to true.

MDN gives the following example.

while (condition)
  statement

If the condition becomes false, statement within the loop stops executing and control passes to the statement following the loop.

The condition test occurs before statement in the loop is executed. If the condition returns true, statement is executed and the condition is tested again. If the condition returns false, execution stops, and control is passed to the statement following while.

To execute multiple statements, use a block statement ({ }) to group those statements.