Comparison Operators

Updated · 7 min read
easyES2026javascriptoperatorsequalitycomparisoninterview

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" == 0 and "" == 0 are both true.
  • <, >, <=, >= compare numbers by value and strings by character code.
  • Objects and arrays are compared by reference: two arrays with the same items are not ===.
  • NaN is not equal to anything, including itself. Test it with Number.isNaN.

The Comparison Operators

OperatorMeaningExampleResult
===Strictly equal (same type and value)120 === 120true
!==Strictly not equal120 !== "120"true
==Loosely equal (converts types first)120 == "120"true
!=Loosely not equal0 != ""false
>Greater than150 > 120true
<Less than150 < 120false
>=Greater than or equal120 >= 120true
<=Less than or equal90 <= 120true

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.

equality.js▶ Try it
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");
Output
true
false
true
true
true
false
true
false
true

Explanation: 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:

strings.js▶ Try it
console.log("apple" < "banana");
console.log("Zebra" < "apple");
console.log("10" < "9");
console.log(10 < 9);
console.log("10" < 9);
Output
true
true
true
false
false

Explanation:

  • 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" < 9 mixes a string and a number, so the string is converted to the number 10, and 10 < 9 is 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?":

objects.js▶ Try it
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" });
Output
false
true
false

Explanation: 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:

nan.js▶ Try it
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));
Output
false
true
true
true
false

Object.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 typesYesNoNo
NaN equals NaNNoNoYes
0 equals -0YesYesNo

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:

form-amount.js▶ Try it
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");
Output
false
true
Full payment

Common 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
puzzle.js▶ Try it
console.log([] == false);
console.log([] === false);
console.log(null == 0);
console.log(null >= 0);
Output
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
full-payment.js▶ Try it
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));
Output
true
false
false

Quick check: Comparison operators

3 questions
1What is 5 === "5"?
2Which is the reliable way to test whether x is NaN?
3What is [1, 2] === [1, 2]?

Key Takeaways

QuestionWrite
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?

Lesson 9 of 139