Assignment Operators

Basic Assignment Operators

OperatorExampleSame As
=x = yx = y
+=x += yx = x + y
-=x -= yx = x - y
*=x *= yx = x * y
/=x /= yx = x / y
%=x %= yx = x % y
**=x **= yx = x ** y

Shift Assignment Operators

OperatorExampleSame As
<x < yx = x << y
>>=x >>= yx = x >> y
>>>=x >>>= yx = x >>> y

Bitwise Assignment Operators

OperatorExampleSame As
&=x &= yx = x & y
^=x ^= yx = x ^ y
|=x |= yx = x | y

Logical Assignment Operators

OperatorExampleSame As
&&=x &&= yx = x && (x = y)
||=x ||= yx = x || (x = y)
??=x ??= yx = x ?? (x = y)

Comparison Operators

Basic Comparison Operators

Given that x = 5, the table below explains the comparison operators:

OperatorDescriptionComparingReturns
==equal tox == 8false
x == 5true
x == “5”true
===equal value and equal typex === 5true
x === “5”false
!=not equalx != 8true
!==not equal value or not equal typex !== 5false
x !== “5”true
x !== 8true
>greater thanx > 8false
<less thanx < 8true
>=greater than or equal tox >= 8false
less than or equal tox 8true

Comparing Different Types

When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison. An empty string converts to 0. A non-numeric string converts to NaN which is always false.

CaseValue
2 < 12true
2 < “12”true
2 < “John”false
2 > “John”false
2 == “John”false
”2” < “12”false
”2” > “12”true
”2” == “12”false

The Nullish Coalescing Operator (??)

The ?? operator returns the first argument if it is not nullish (null or undefined).
Otherwise it returns the second argument.

let name = null;
let text = "missing";
let result = name ?? text;

The Optional Chaining Operator (?.)

The ?. operator returns undefined if an object is undefined or null (instead of throwing an error).

// Create an object:
const car = { type: "Fiat", model: "500", color: "white" };
// Ask for car name:
document.getElementById("demo").innerHTML = car?.name;