Expression and Operator

Primary expressions


Celsius to Fahrenheit conversion

Option 1

let fahrenheit = 50;
let celsius = fahrenheit - 32 / 1.8;
console.log(celsius); //=> 32.2

Option 2

// grouping operator
let fahrenheit = 50;
let celsius = (fahrenheit - 32) / 1.8;
console.log(celsisu); //=> 10

References:

Left-hand-side expressions


// object.property
let obj = { number: 10 };
console.log(obj.number); //=> 10
// spread operator
let numbers = [1, 2, 3];
console.log(...numbers, 4, 5); //=> [1, 2, 3, 4, 5]

Increment and decrement


// postfix increment
let number = 10;
console.log(number++); //=> 10
// prefix increment
let number = 10;
console.log(++number); //=> 11
// postfix decrement
let number = 10;
console.log(number--); //=> 10
// prefix decrement
let number = 10;
console.log(--number); //=> 9

Unary operators


// delete operator
let numbers = [1, 2, 3];
console.log(numbers); //=> [ 1, 2, 3 ]

delete numbers[1];
console.log(numbers); //=> [ 1, , 3 ]
// typeof operator
let number = 1;
console.log(typeof number); //=> number

let name = 'Lorem ipsum';
console.log(typeof name); //=> string

let numbers = [1, 2, 3];
console.log(typeof numbers); //=> object
// unary plus operator
let number = '15';
console.log(number); //=>  "15"
console.log(+number); //=>  15
// unary negation operator
let number = -15;
console.log(number); //=>  -15 11111111111111111111111111110001
console.log(-number); //=>  15 00000000000000000000000000001111
// bitwise not
let number = 16;
console.log(number); //=>   16 00000000000000000000000000010000
console.log(~number); //=> -17 11111111111111111111111111101111
// logical not
let number = 16;
console.log(!number); //=> false

Arithmetic operators


// addition operator
console.log(10 + 3); //=> 13
console.log(true + 3); //=> 4
// subtraction operator
console.log(10 - 3); //=> 7
console.log(1.4 - 1.2); //=> 0.19999999999999996 (IEEE 754)
// multiplication operator
console.log(10 * 3); //=> 30
// division operator
console.log(10 / 3); //=> 3.3333333333333335
console.log(parseInt(10 / 3)); //=> 3 - Integer division
console.log(Math.trunc(10 / 3)); //=> 3 - Integer division
// remainder operator
console.log(10 % 3); //=> 1
// exponentiation operator
console.log(10 ** 3); //=> 1000  Math.pow(10, 3)

Relational operators


// in operator
let numbers = [1, 2, 3];
console.log(0 in numbers); //=> true
console.log(1 in numbers); //=> true
console.log(3 in numbers); //=> false
console.log('value' in { value: 1 }); //=> true
console.log('PI' in Math); //=> true
// instanceof operator
console.log('' instanceof String); //=>           false
console.log(new String() instanceof String); //=> true
// less than operator
console.log(1 < 10); //=> true
// less than or equal operator
console.log(1 <= 10); //=> true
// greater than operator
console.log(10 > 1); //=> true
// greater than or equal operator
console.log(10 >= 1); //=> true

Equality operators


false == false, 0, “0”, “”, [], [0]

// equality operator: type–converting comparisons
console.log(1 == 1); //=>     true
console.log(1 == '1'); //=>   true
console.log(1 == '1a'); //=>  false
console.log(1 == true); //=>  true
console.log(0 == false); //=> true
// inequality operator
console.log(1 != 'a'); //=>   true
console.log(1 != false); //=> true
console.log(1 != true); //=>  false
// identity/strict equality: strict and type–converting comparisons
console.log(1 === 1); //=>     true
console.log(1 === '1'); //=>   false
console.log('1' === '1'); //=> true
// non-identity
console.log(1 !== '1'); //=>   true
console.log('' !== true); //=> true
console.log(1 !== 1); //=>     false

References:

Bitwise shift operators


Bitwise operators treat their operands as a sequence of 32 bits
-(2 ** 32) to 2 ** 31 - 1
-2147483648 to 2147483647

// left shitf operator - each step means "multiply by two"
console.log(9); //=>      00000000000000000000000000001001   9
console.log(9 << 2); //=> 00000000000000000000000000100100  36
// sign-propagating right shift operator - each step means "divide by two"
console.log(9); //=>       00000000000000000000000000001001   9
console.log(9 >> 2); //=>  00000000000000000000000000000010   2
console.log(-9); //=>      11111111111111111111111111110110  -9
console.log(-9 >> 2); //=> 11111111111111111111111111111101  -3
// zero-fill right shift operator
console.log(9); //=>        00000000000000000000000000001001   9
console.log(9 >>> 2); //=>  00000000000000000000000000000010   2
console.log(-9); //=>       11111111111111111111111111110110  -9
console.log(-9 >>> 2); //=> 00111111111111111111111111111101  1073741821

Decoding Mantis green ARGB

Binary bitwise operators


// bitwise and operator
console.log(9); //=>      00000000000000000000000000001001   9
console.log(14); //=>     00000000000000000000000000001110  14
console.log(14 & 9); //=> 00000000000000000000000000000100   8
// bitwise or operator
console.log(9); //=>      00000000000000000000000000001001   9
console.log(14); //=>     00000000000000000000000000001110  14
console.log(14 | 9); //=> 00000000000000000000000000001111  15
// bitwise xor operator
console.log(9); //=>      00000000000000000000000000001001   9
console.log(14); //=>     00000000000000000000000000001110  14
console.log(14 ^ 9); //=> 00000000000000000000000000000101   7

Binary logical operators


false == false, 0, “”, null, undefined, NaN

// logical and operator
console.log(true && true); //=> true
console.log(true && 10); //=>   10
console.log(false && 10); //=>  false
// logical or operator
console.log(false || false); //=> false
console.log(false || true); //=>  true
console.log(false || 10); //=>    10

Precedence

// Logical AND > Logical OR
console.log((false && true) || true); //=> true
console.log(false && (true || true)); //=> false

Value Default

let variable;
let temp = variable || 10;
console.log(temp); //=> 10

let variable = 1;
let temp = variable || 10;
console.log(temp); //=> 1

Other examples

Conditional operator (ternary)


// conditional operator
let number = 10;
let kind = number % 2 == 0 ? 'even' : 'odd';
console.log(kind); //=> 'even'

Assignment operators


// assign operator
let variable = 10;
console.log(variable); //=> 10

Shorthand: *=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, ^=, |=

// addition assignment operator
let variable = 10; // 10
variable += 1; // variable = variable + 1
console.log(variable); //=> 11

Destructuring Assignment, Deep Matching

Comma operator


// comma operator
let x, y;

Recap


Groups

Operator type Operator Operator Name
Arithmetic Operators … + … Addition
… / … Division
… ** … Exponentiation
… * … Multiplication
… % … Remainder
… - … Subtraction
… ++ Postfix Increment
… -- Postfix Decrement
-- … Prefix Decrement
++ … Prefix Increment
- … Unary negation
+ … Unary plus
Assignment Operators … += … Addition assignment
… &= … Bitwise AND assignment
… |= … Bitwise OR assignment
… ^= … Bitwise XOR assignment
… /= … Division assignment
… **= … Exponentiation assignment
… <<= … Left shift assignment
… *= … Multiplication assignment
… %= … Remainder assignment
… >>= … Right shift assignment
… = … Assignment
… -= … Subtraction assignment
… >>>= … Unsigned right shift assignment
Bitwise Operators … & … Bitwise AND
… << … Bitwise left shift
~ … Bitwise NOT
… | … Bitwise OR
… >> … Bitwise right shift
… >>> … Bitwise unsigned right shift
… ^ … Bitwise XOR
Comparison Operators … == … Equality
… != … Inequality
… === … Identity
… !== … Non-identity
… > … Greater than
… >= … Greater than or equal
… < … Less than
… <= … Less than or equal
Logical Operators … && … Logical AND
… || … Logical OR
! … Logical NOT
Conditional Operator … ? … : … Conditional / Ternary
Comma Operator … , … Comma / Sequence

Operators List

Operator type Operators
Primary expressions this, function, class, function*, yield, yield*, async function*, await, [], {}, /ab+c/i, ( )
Left-hand-side expressions object.property, object["property"], new, new.target, super, ...obj
Increment and decrement A++, A--, ++A, --A
Unary operators delete, void, typeof, +, -, ~, !
Arithmetic operators +, -, *, /, %, **
Relational operators in, instanceof, <, <=, >, >=
Equality operators ==, !=, ===, !==
Bitwise shift operators <<, >>, >>>
Binary bitwise operators &, |, ^
Binary logical operators &&, ||
Assignment operators =, *=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, ^=, |=, [a, b] = [1, 2], {a, b} = {a:1, b:2}
Conditional operator (condition ? ifTrue : ifFalse)
Comma operator ,