Expressions & Operators

JavaScript expressions and operators.

Arithmetic

+
Addition
const newNumber = 1 + 1; // 2
const newString = "1" + "1"; // '11'
-
Subtraction
const newNumber = 1 - 1; // 0
const newString = "1" - "1"; // 0 - number-like strings are 'coerced' into numbers
const newString = "1" - "one"; // NaN (or 'Not-a-Number')
/
Division
const newNumber = 1 / 2; // 0.5
const newString = "1" / "2"; // 0.5
const newString = "1" / "two"; // NaN
*
Multiplication
const newNumber = 1 * 2; // 2
const newString = "1" * "2"; // 2
const newString = "1" * "two"; // NaN
%
Remainder (Modulo)
const newNumber = 2.3 % 1; // 0.3
const newString = "2.3" % "1"; // 0.3
const newString = "2.3" % "one"; // NaN
**
Exponentiation
const newNumber = 2 ** 3; // 8
const newString = "2" ** "3"; // 8
const newString = "2" ** "three"; // NaN

Relational

in
Object contains property (looking at keys only)
const hasSomething = "something" in { something: 1, another: 2 }; // true
const hasElephant = "something" in { something: 1, another: 2 }; // false
const hasOne = 1 in { something: 1, another: 2 }; // false
<
Less-than
const isSmall = 1 < 2; // true
const notSmall = 1 < 1; // false
const notSmall = 1 < 0; // false
<=
Less-than-or-equal-to
const isSmall = 1 <= 2; // true
const notSmall = 1 <= 1; // true
const notSmall = 1 <= 0; // false
>
Greater-than
const isSmall = 1 > 2; // false
const notSmall = 1 > 1; // false
const notSmall = 1 > 0; // true
>=
Greater-than-or-equal-to
const isSmall = 1 >= 2; // false
const notSmall = 1 >= 1; // true
const notSmall = 1 >= 0; // true

Equality

==
Equality (allowing coercion)
const notEqual = 1 == 2; // false
const equalNumbers = 1 == 1; // true
const similarString = 1 == "1"; // true
const similarBool = 1 == true; // true
!=
Inequality (allowing coercion)
const notEqual = 1 != 2; // true
const equalNumbers = 1 != 1; // false
const similarString = 1 != "1"; // false
const similarBool = 1 != true; // false
===
Identity (disallowing coercion)
const notEqual = 1 === 2; // false
const equalNumbers = 1 === 1; // true
const similarString = 1 === "1"; // false
const similarBool = 1 === true; // false
!==
Nonidentity (disallowing coercion)
const notEqual = 1 !== 2; // true
const equalNumbers = 1 !== 1; // false
const similarString = 1 !== "1"; // true
const similarBool = 1 !== true; // true

Logical

&&
Logical AND
const allTrue = true && true; // true
const someFalse = false && true; // false
const allFalse = false && false; // false
||
Logical OR (also a falsy-coalescing operator)
const allTrue = true || true; // true
const someFalse = false || true; // true
const allFalse = false || false; // false

// Falsy-Coalescing is used to return the first non-falsy value
const firstIsGood = "first" || "second"; // 'first'
const firstIsUndefined = undefined || "second"; // 'second'
const firstIsNull = null || "second"; // 'second'
const firstIsFalse = false || "second"; // 'second'
const firstIs0 = 0 || "second"; // 'second'

Assignment

=
Assignment
const a = 1; // 1
const b = a; // 1
*=
Multiplication assignment
const a = 2; // 2
const a *= 2; // 4 (a = a * 2)
/=
Division assignment
const a = 2; // 2
const a /= 2; // 1 (a = a / 2)
+=
Addition assignment
const a = 2; // 2
const a += 2; // 4 (a = a + 2)
-=
Subtraction assignment
const a = 2; // 2
const a -= 2; // 0 (a = a - 2)
[a, b] = [1, 2], {a, b} = {a: 1, b: 2}
Destructuring assignment
const anArray = [1, 2];
const anObject = { aKey: 1, bKey: 2 };
const [first, second] = anArray; // first = 1, second = 2
const { aKey } = anObject; // aKey = 1
,
Evaluate multiple, return last
const result = ((x = 1), (x += 2), (x /= 4), x); // 0.75

Miscellaneous

(condition ? ifTrue : ifFalse)
ternary operator
const getFirst = 1 >= 0 ? "first" : "second"; // 'first'
const getSecond = 1 >= 2 ? "first" : "second"; // 'second'
?.
optional chaining operator
const something = {
  nullChild: null;
};
// Note: (`something.child` is `undefined`)
const undefinedChainOkay = something.child?.grandchild; // returns `undefined`
const undefinedChainError = something.child.grandchild; // throws error (trying to access `grandchild` of `undefined`)

const nullChainOkay = something.nullChild?.grandchild; // returns `undefined`
const nullChainError = something.nullChild.grandchild; // throws error (trying to access `grandchild` of `null`)