Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 156 ยท Operators

Comparison and Equality

Compare values in JavaScript with ===, !==, <, > and Object.is. Learn why == surprises, how strings compare, and why objects are compared by reference.

Programs constantly ask questions: Is the password long enough? Did the user pick this option? Is this order newer than that one? Each of those questions is a comparison, and every comparison operator gives back a boolean โ€” true or false โ€” ready to use in an if.

JavaScript has more ways to check equality than most languages, and they don't all agree. This lesson shows which operator to reach for, how strings and numbers are ordered, the strange case of NaN, and why two objects that look identical are not equal.

The comparison operators

Operator Question it asks
=== Same type and same value? (strict equality)
!== Different type or different value?
== Equal after type conversion? (loose equality)
!= Not equal after type conversion?
< / > Less than / greater than?
<= / >= Less than or equal / greater than or equal?
JS Comparisons give booleans

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Strict equality: === and !==

Strict equality is the comparison you should use by default. a === b is true only when both values have the same type and the same value. It never converts anything:

JS Strict equality

Press Run (or Ctrl + Enter) to see the console output here.

No output.

All of these are what you'd expect, except the last two โ€” we'll come back to NaN and -0 shortly.

Loose equality: == and !=

Loose equality first converts the two values to a common type, then compares. You saw the rules in Type Conversion and Coercion, along with their famous oddities:

JavaScript
5 == "5";            // true  โ€” the string becomes a number
0 == "";             // true
false == "0";        // true
null == undefined;   // true
null == 0;           // false

These conversions can hide real bugs: a form value "0" and the number 0 are "equal", an empty string equals zero, and so on. That's why most teams and linters ban == and !=, with a single common exception: value == null, which is a deliberate shortcut for "is null or undefined" (see null and undefined).

Rule of thumb: always use === and !==. If the two values might have different types โ€” for instance a string from a form and a number in your code โ€” convert one of them explicitly first: Number(input) === 42.

Ordering with <, >, <= and >=

For numbers these work as in math. The interesting cases are strings and mixed types:

JS Comparing strings and mixed types

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The rules:

  • Both strings: compared character by character using their UTF-16 code units. That makes all uppercase letters sort before all lowercase letters, and digits sort as text, so "10" < "9".
  • Anything else: both sides are converted to numbers. If either becomes NaN, every comparison returns false โ€” note that "abc" < 5 and "abc" >= 5 are both false.

When you need to order text the way a person would โ€” ignoring case, handling accents, sorting item9 before item10 โ€” use localeCompare(). It returns a negative number, zero or a positive number, which is exactly what sorting needs. You'll use it in Sorting Arrays.

The special cases: NaN and -0

NaN is the only value in JavaScript that is not equal to itself. NaN === NaN is false, and so is NaN == NaN. That means value === NaN can never work as a check. Use Number.isNaN(value) instead:

JavaScript
const result = Number("oops");

result === NaN;          // false โ€” always!
Number.isNaN(result);    // true

Be careful not to confuse it with the older global isNaN(). The global version converts its argument to a number first, so isNaN("hello") is true even though "hello" is a string, not NaN. Number.isNaN() doesn't convert and only returns true for the actual NaN value.

The second oddity is negative zero. Floating-point numbers have a separate -0, produced by things like -1 * 0 or rounding a small negative number. === treats 0 and -0 as equal, which is almost always what you want.

Object.is()

Object.is(a, b) is a third equality check. It behaves like === except for exactly those two special cases:

JS Object.is versus ===

Press Run (or Ctrl + Enter) to see the console output here.

No output.

You'll rarely need Object.is() in everyday code. It's used inside libraries that must detect any change of value โ€” for example, frameworks that check whether state really changed. Array methods such as includes() use a similar rule that treats NaN as equal to itself, which is why [NaN].includes(NaN) is true while [NaN].indexOf(NaN) is -1.

Objects are compared by reference

All the operators above compare primitive values by their content. Objects โ€” including arrays and functions โ€” work differently. A variable holding an object holds a reference to it, like an address. Two comparisons of objects ask "is this the same object?", not "do they look the same?":

JS Comparing objects and arrays

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Two object literals always create two different objects, so {} === {} and [] === [] are false. To compare contents, compare the properties you care about. Turning simple data into JSON strings and comparing those works for quick checks, but it's sensitive to property order and doesn't handle every value type. You'll learn about references and copies properly in Copying Objects: Shallow and Deep.

A common bug: if (list === []) is always false, even for an empty array, because it compares against a brand-new array. Check list.length === 0 instead.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does "5" === 5 evaluate to?

    Strict equality requires the same type. A string is never strictly equal to a number.
  2. 2. Why is "10" < "9" true?

    When both sides are strings, JavaScript compares text, not numeric value. Convert to numbers for numeric ordering.
  3. 3. How do you reliably check whether x is NaN?

    NaN is never equal to anything, including itself, so === and == always return false. typeof NaN is "number".
  4. 4. Given const a = [1]; const b = [1]; const c = a;, which is true?

    Objects compare by reference. Only a and c point to the same array.
  5. 5. Where do Object.is() and === give different answers?

    Object.is considers NaN equal to itself and 0 different from -0; otherwise it matches ===.

Summary

  • Comparison operators return booleans; use === and !== by default.
  • == converts types first and has surprising results; the common exception is value == null.
  • Two strings compare character by character ("10" < "9"); otherwise values are converted to numbers, and NaN makes every comparison false.
  • NaN is not equal to itself โ€” use Number.isNaN(). Object.is() differs from === only for NaN and -0.
  • Objects and arrays are compared by reference: two separate objects are never equal, even with identical contents.

Next: Logical Operators and Short-Circuiting โ€” combining conditions with &&, || and !.