Sub topic 2
Comparing Values: === vs == and Relational Operators
This chapter assumes you know how to declare variables with let and const and that you are familiar with JavaScript's primitive types, arrays, and plain objects. Every condition you write later in this tutorial depends on comparisons, so you need to know exactly what each operator does.
Strict Equality: ===
The strict equality operator === returns true only when both operands have the same type and the same value. It never converts types. Its counterpart !== is the strict "not equal" operator.
console.log(5 === 5); // true
console.log(5 === "5"); // false: number vs string
console.log(true === 1); // false: boolean vs number
console.log("hi" === "hi"); // trueLoose Equality: ==
The loose equality operator == converts operands to a common type before comparing when their types differ. This conversion is called type coercion. The rules are short, but their results are often surprising.
console.log(0 == ""); // true
console.log(0 === ""); // false
console.log(null == undefined); // true
console.log(null === undefined); // false
console.log("1" == 1); // true
console.log(false == 0); // true
console.log(null == 0); // falseThe diagram below shows the decision path == follows, in simplified form.
Two Special Cases: NaN and Objects
NaN is the only value that is not equal to itself, under either operator. Use Number.isNaN() to test for it. Object.is() behaves like === except that it treats NaN as equal to NaN and distinguishes 0 from -0.
console.log(NaN === NaN); // false
console.log(Number.isNaN(NaN)); // true
console.log(Object.is(NaN, NaN)); // true
console.log(Object.is(0, -0)); // falseObjects and arrays compare by reference. Two separate arrays with identical contents are not equal, because they are different objects in memory.
const a = [1, 2];
const b = [1, 2];
const c = a;
console.log(a === b); // false
console.log(a === c); // trueRelational Operators: <, >, <=, >=
The relational operators have no strict version, so they always coerce. When both operands are strings, they compare character by character using UTF-16 code units. Otherwise, both operands are converted to numbers.
console.log("apple" < "banana"); // true
console.log("Z" < "a"); // true: uppercase codes are lower
console.log("10" < "9"); // true: string comparison
console.log(10 < "9"); // false: numeric comparison
console.log(null >= 0); // true: null becomes 0
console.log(undefined > 0); // false: undefined becomes NaNCommon errors come from these rules:
| Mistake | Result | Fix |
|---|---|---|
| Comparing numeric strings from form inputs | `"10" < "9"` is `true` | Convert first with `Number(value)` |
| Testing `x === NaN` | Always `false` | Use `Number.isNaN(x)` |
| Comparing arrays with `===` | `false` for equal contents | Compare elements, or compare the same reference |
| Assuming `null >= 0` means `null == 0` | `>=` is `true`, `==` is `false` | Check for `null` explicitly |
Any comparison involving NaN returns false, so NaN < 1 and NaN >= 1 are both false.
Exercise
Predict the output of each line: console.log("5" == 5, "5" === 5, "b" > "a", "2" > "12", [] === []);
Show answer
true false true true false. Loose equality turns "5" into 5. Strict equality sees different types. "b" comes after "a" in character order. "2" > "12" compares strings, and "2" comes after "1". The two array literals are different objects.
Exercise
Write a condition that is true when value is either null or undefined, but not when it is 0 or "".
Show answer
value == null, or value === null || value === undefined if you want to stay strict. Loose equality treats null and undefined as equal only to each other, so 0 and "" do not match.