Lesson 34 of 296
= vs == vs ===
What you will learn
- What each of the three signs does
- Strict and loose equality
- Why you should almost always use
=== - The not-equal signs
- How objects and arrays are compared
On this page
Three different signs
| Sign | Name | Job |
|---|---|---|
= | Assignment | Puts a value into a variable |
== | Loose equality | Asks "equal?" after converting types |
=== | Strict equality | Asks "equal?" with no conversion: value and type must match |
= puts a value in a box
let age = 30;It does not compare anything. It means "store 30 in age".
=== strict equality
5 === 5 true
5 === "5" false (number vs string)
0 === false false (number vs boolean)
null === undefined false
"hi" === "hi" trueBoth the type and the value must be the same.
== loose equality
== first tries to convert both sides to the same type, then compares. The rules are complicated:
5 == "5" true ("5" becomes 5)
0 == false true (false becomes 0)
"" == false true (both become 0)
null == undefined true (special rule)
null == 0 false (special rule)
[] == false true ([] becomes "" then 0; false becomes 0)Most developers cannot remember all of these rules, which is why == causes bugs.
The rule
Always use === and !==.
The one exception some teams allow:
if (value == null) { ... }This is true when value is null or undefined, and false for everything else. It is a short way to check for "no value". Many teams still prefer to write both checks with ===, or use ?? (Part 5).
Not equal
| Sign | Meaning |
|---|---|
!== | Strictly not equal (use this) |
!= | Loosely not equal (avoid) |
5 !== "5" true
5 != "5" falseThe assignment bug
let role = "user";
if (role = "admin") {
console.log("Welcome, admin");
}This always prints "Welcome, admin". role = "admin" puts "admin" into role; it does not compare. A non-empty string counts as true, so the if runs. It should be role === "admin". Tools such as ESLint warn about this.
NaN
NaN === NaN false
NaN == NaN false
Number.isNaN(NaN) true
Object.is(NaN, NaN) trueObject.is() is like === with two differences: it says NaN is equal to NaN, and it says 0 and -0 are different. You rarely need it, but it is a known interview question.
Comparing objects and arrays
Objects and arrays are compared by reference, which means "are these the very same object in memory?", not "do they look the same?".
[1, 2] === [1, 2] false (two different arrays)
{ a: 1 } === { a: 1 } false (two different objects)
const a = [1, 2];
const b = a;
a === b true (b points to the same array)Each [ ] or { } creates a new object, even if the contents are the same. To compare contents, you must check each value (Part 6 teaches "deep equal").
Common mistakes
- Using
=insideifwhen you meant===. - Using
==and getting surprisingtrueresults. - Comparing with
NaNusing===. - Expecting two arrays with the same contents to be
===.
How to say it in an interview
"Single equals assigns a value. Double equals compares after type coercion, so 5 == '5' is true. Triple equals compares value and type without coercion, so 5 === '5' is false. I always use triple equals, except sometimes value == null to catch both null and undefined. Objects and arrays are compared by reference, so two arrays with the same contents are not equal."
Key words
| Word | Meaning |
|---|---|
| Assignment | Storing a value with = |
| Loose equality | ==, compares after converting types |
| Strict equality | ===, compares value and type with no conversion |
| Reference | Which object in memory a variable points to |
| Object.is | A comparison like === that treats NaN as equal to itself |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.