reading-notes

Operators and Loops in JavaScript

Expressions & Operators - Web Docs

Loops & Iteration

Arithmetic Operators

Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponential
/ Division
% Modulus (Division Remainder)
++ Increment
Decrement

Assignment Operators

Operator Example Same As
= x = y x = y
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y
**= x **= y x = x ** y

Adding JS Strings

let text1 = "John";
let text2 = "Doe";
let text3 = text1 + " " + text2;

// output of text3 = 'John Doe'

Comparison Operators

Operator Description
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
? ternary operator

Logical Operators

Operator Description
&& logical and
|| logical or
! logical not

Type Operators

Operator Description
typeof Returns the type of a variable
instanceof Returns true if an object is an instance of an object type

Bitwise Operators

Operator Description Example Same As Result Decimal
& AND 5 & 1 0101 & 0001 0001 1
| OR 5 | 1 0101 | 0001 0101 5
~ NOT ~ 5 ~0101 1010 10
^ XOR 5 ^ 1 0101 ^ 0001 0100 4
« left shift 5 « 1 0101 « 1 1010 10
» right shift 5 » 1 0101 » 1 0010 2
»> unsigned right shift 5 »> 1 0101 »> 1 0010 2

Loops and Iteration

For Statement

Syntax:

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

Example:

let countDown = 10 // Set initial condition in variable
for (let i = countDown; i >= 0; i--) // While i >= 0, decrement by 1, then exit the loop
  {
    console.log(`${i} second${i !== 1 ? 's' : ''} until lift off!`) // Logs count of i
   }
console.log('Lift Off!') // When i < 0, log 'Lift Off!'

Do…While Statement

Syntax:

do
  statement
while (condition);

Example:

let i = 0; // Set initial condition
do {  // Do the statement below
  i += 1;
  console.log(i);
} while (i < 5); // While i is < 5

While Statement

Syntax:

while (condition)
  statement

Example:

let i = 10
while (i >= 0){ // While i is >= 0, the statement will execute
  console.log(`Countdown: ${i}`) // First log value of i
  i-- // Then decrement i by 1
}
console.log('Blast off!') // When i drops below 0, log 'Blast Off'