Comparison Operators
A comparison operator compares two values and returns a boolean, true or false. Comparisons drive every decision a program makes: is the balance above zero, is the payment late, is this the student we're looking for. JavaScript has two kinds of equality, strict (===) and loose (==), and choosing the right one prevents a whole family of bugs.
In One Minute
===(strict equality) is true only when the values are equal and of the same type. Use it always.==(loose equality) converts types before comparing, so"0" == 0and"" == 0are bothtrue.<,>,<=,>=compare numbers by value and strings by character code.- Objects and arrays are compared by reference: two arrays with the same items are not
===. NaNis not equal to anything, including itself. Test it withNumber.isNaN.
The Comparison Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
=== | Strictly equal (same type and value) | 120 === 120 | true |
!== | Strictly not equal | 120 !== "120" | true |
== | Loosely equal (converts types first) | 120 == "120" | true |
!= | Loosely not equal | 0 != "" | false |
> | Greater than | 150 > 120 | true |
< | Less than | 150 < 120 | false |
>= | Greater than or equal | 120 >= 120 | true |
<= | Less than or equal | 90 <= 120 | true |
Strict vs Loose Equality
Strict equality (===) compares without converting. If the types differ, the answer is false, full stop. Loose equality (==) first tries to convert both values to the same type, following a long list of rules, and then compares.
console.log(120 === 120);
console.log(120 === "120");
console.log(120 == "120");
console.log(0 == "");
console.log(0 == "0");
console.log("" == "0");
console.log(null == undefined);
console.log(null === undefined);
console.log(false == "0");true
false
true
true
true
false
true
false
trueExplanation: look at the middle three lines. 0 == "" is true and 0 == "0" is true, yet "" == "0" is false. Loose equality isn't even consistent with itself, because each comparison converts differently: the two strings are compared as strings (different), while each string compared with 0 is converted to a number ("" becomes 0, "0" becomes 0).
Comparing Numbers and Strings
Numbers compare by value. Strings compare character by character using each character's code (its position in Unicode), which gives alphabetical order for plain lowercase or uppercase words, but not in general:
console.log("apple" < "banana");
console.log("Zebra" < "apple");
console.log("10" < "9");
console.log(10 < 9);
console.log("10" < 9);true
true
true
false
falseExplanation:
- Every uppercase letter comes before every lowercase letter in Unicode, so
"Zebra" < "apple". "10" < "9"compares two strings:"1"comes before"9", so it is true. This is why sorting numbers as text puts 10 before 9."10" < 9mixes a string and a number, so the string is converted to the number 10, and10 < 9is false.
For sorting names the way people expect (ignoring case, handling accents), use a.localeCompare(b), covered in the string search lesson.
Objects Compare by Reference
Two separate objects are never equal, even with identical contents. === asks "is this the same object?", not "do these look the same?":
const a = [120, 80];
const b = [120, 80];
const c = a;
console.log(a === b);
console.log(a === c);
console.log({ name: "Navika" } === { name: "Navika" });false
true
falseExplanation: a and b are two different arrays that happen to hold the same numbers. c = a doesn't copy the array; it makes c point to the same one, so a === c is true. To compare contents, compare the items (or, for simple data, JSON.stringify(a) === JSON.stringify(b)); the lesson on copying and comparing objects goes further.
NaN and Object.is
NaN is the only value in JavaScript that is not equal to itself:
const result = Number("abc");
console.log(result === NaN);
console.log(Number.isNaN(result));
console.log(Object.is(result, NaN));
console.log(0 === -0);
console.log(Object.is(0, -0));false
true
true
true
falseObject.is(a, b) is a third kind of equality. It works like === except for two cases: it says NaN equals NaN, and it says 0 and -0 are different. You'll rarely need it in everyday code, but frameworks use it to detect changes, and it answers interview questions about these edge cases.
== | === | Object.is | |
|---|---|---|---|
| Converts types | Yes | No | No |
NaN equals NaN | No | No | Yes |
0 equals -0 | Yes | Yes | No |
In Practice
The Fee Tracker reads an amount from a form field, which is always a string. Comparing it to a number with === would always fail, so the code converts first, then compares strictly:
const amountField = "120"; // what a text input gives you
const expectedFee = 120;
console.log(amountField === expectedFee); // wrong: string vs number
const amount = Number(amountField);
console.log(amount === expectedFee); // right: convert, then compare
console.log(amount >= 100 ? "Full payment" : "Partial payment");false
true
Full paymentCommon Mistakes
Using = instead of ===. if (fee = 0) assigns 0 and the condition is false; it doesn't compare. Linters flag this.
Testing for NaN with ===. It's always false. Use Number.isNaN(x).
Comparing arrays with ===. Different arrays are never equal. Compare their contents.
Comparing numbers stored as strings. "100" < "20" is true. Convert to numbers first.
Interview Corner
What is the difference between == and ===?
=== compares type and value without conversion. == applies the abstract equality algorithm: null and undefined equal each other only, booleans become numbers, strings compared with numbers become numbers, and objects are converted to primitives. That makes == non-transitive (0 == "", 0 == "0", but "" != "0"). Use ===.
Why is NaN === NaN false?
IEEE 754, the floating-point standard JavaScript numbers follow, defines NaN as unequal to every value, including itself, because it represents an undefined result. Test with Number.isNaN(x) or Object.is(x, NaN). The global isNaN converts its argument first, so isNaN("abc") is true; prefer Number.isNaN.
How are objects compared in JavaScript?
By reference identity. === is true only if both operands refer to the same object in memory. Structural comparison needs a manual or library deep-equal.
What does this print?
Show solution
console.log([] == false);
console.log([] === false);
console.log(null == 0);
console.log(null >= 0);true
false
false
true[] == false converts both sides to numbers: [] becomes "" then 0, and false becomes 0. null == 0 is false because == treats null specially (it only equals undefined), but >= converts null to 0, so null >= 0 is true. One more reason to use ===.
Practice
Challenge: Write isFullPayment(input, fee) that takes a form value (a string) and a fee (a number), and returns true only if the input is a valid number equal to or greater than the fee. Test it with "120", "80" and "abc" against a fee of 120.
Show solution
function isFullPayment(input, fee) {
const amount = Number(input);
if (Number.isNaN(amount)) return false;
return amount >= fee;
}
console.log(isFullPayment("120", 120));
console.log(isFullPayment("80", 120));
console.log(isFullPayment("abc", 120));true
false
falseKey Takeaways
| Question | Write |
|---|---|
| Same value and type? | a === b |
| Different? | a !== b |
Is it NaN? | Number.isNaN(x) |
| Same object? | a === b (objects) |
| Alphabetical order? | a.localeCompare(b) |
Was this tutorial helpful?