Given that x = 5, the table below explains the comparison operators:
Operator
Description
Comparing
Returns
==
equal to
x == 8
false
x == 5
true
x == “5”
true
===
equal value and equal type
x === 5
true
x === “5”
false
!=
not equal
x != 8
true
!==
not equal value or not equal type
x !== 5
false
x !== “5”
true
x !== 8
true
>
greater than
x > 8
false
<
less than
x < 8
true
>=
greater than or equal to
x >= 8
false
⇐
less than or equal to
x ⇐ 8
true
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.
Case
Value
2 < 12
true
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;