Skip to main content

JavaScript Type System

JavaScript == vs ===

Written by Published

The double equals converts types before comparing; the triple equals does not.

This is one of the most asked JavaScript interview questions.

The short advice is simple: use === unless you have a reason not to.

Example

Example

javascript

console.log("5" == 5);
console.log("5" === 5);

The output is true then false.

== converted the string; === compared the types too.

Loose and Strict Equality

== is loose equality: it converts types, then compares.

=== is strict equality: different types are never equal.

!= and !== are the matching opposites.

Syntax

Syntax

javascript

a == b    // loose
a === b   // strict

Prefer strict equality by default.

Why Loose Equality Surprises People

The conversion rules produce some results nobody expects.

Example

Example

javascript

console.log(0 == "");
console.log(0 == "0");
console.log("" == "0");

The first two are true and the last is false.

Equality here is not even consistent between the three.

Strict Equality Is Predictable

Different types are simply not equal, with no conversion at all.

Example

Example

javascript

console.log(0 === "");
console.log(0 === "0");
console.log(0 === 0);

Only the last is true.

The One Useful Case for ==

null == undefined is true, and they are not equal to anything else.

So value == null is a neat check for either.

Example

Example

javascript

console.log(null == undefined);
console.log(null === undefined);
console.log(null == 0);

The output is true, false, false.

Objects Compare by Identity

Two objects are equal only if they are the same object.

This is true for both operators.

Example

Example

javascript

const a = { id: 1 };
const b = { id: 1 };

console.log(a == b);
console.log(a === a);

Contents are never compared, however alike they look.

Comparing Values You Do Not Control

Convert first, then compare strictly.

Example

Example

javascript

const entered = "42";

console.log(Number(entered) === 42);

Your intention is obvious and the result is predictable.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript == vs ===</title>
</head>
<body>

  <h1>== vs ===</h1>

  <p id="out"></p>

  <script>
    const lines = [
      '"5" == 5   is ' + ("5" == 5),
      '"5" === 5  is ' + ("5" === 5),
      '0 == ""    is ' + (0 == ""),
      '0 === ""   is ' + (0 === ""),
      'null == undefined  is ' + (null == undefined)
    ];

    document.getElementById("out").innerHTML = lines.join("<br>");
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try more comparisons:

Add false == "0" and see what happens.

Important Points

  • == converts types before comparing.
  • === compares the type as well as the value.
  • Loose equality gives inconsistent-looking results.
  • value == null usefully catches null and undefined.
  • Objects are compared by identity, never by contents.

Conclusion

Use === by default and the surprises disappear.

The one exception, checking for null or undefined together, is worth knowing.

This is among the most reliably asked interview questions in JavaScript.