01 / 20
== ===

== vs. ===
& Coercion

Master JavaScript's two equality operators, understand how type coercion works behind the scenes, and learn why strict equality prevents subtle bugs.

JavaScript

What Is Equality in JavaScript?

Every programming language needs a way to compare values. JavaScript gives you two different operators to check if things are "equal":

==
Abstract Equality: compares values after converting types to match. Also called "loose equality."
===
Strict Equality: compares values without any type conversion. Both type and value must match.

JavaScript's Type System

Every value in JavaScript has a type. When you use ==, JavaScript may silently convert one type to another. Here are the key primitive types:

Number
42, 3.14, NaN
String
"hello", "42", ""
Boolean
true, false
Special
null, undefined

What Is Type Coercion?

Type coercion is JavaScript's automatic process of converting a value from one type to another when an operator expects a different type.

"5" String coercion 5 Number == 5 Number true "5" == 5

The Core Rule

// Loose equality: converts types, then compares
"5" == 5   // true (string → number)

// Strict equality: NO conversion, compares type + value
"5" === 5  // false (string ≠ number)
Use == (loose) Are the types the same? YES Compare values NO Coerce types to match Then compare values Use === (strict) Types different? → false Same? → compare values

How Coercion Works with ==

When == encounters different types, it follows these coercion rules in order:

1
String vs Number: convert the string to a number. "42" → 42
2
Boolean vs Anything: convert the boolean to a number first. true → 1, false → 0
3
null == undefined: these two are equal to each other and nothing else.
4
Object vs Primitive: convert the object using valueOf() or toString().

== vs ===: Side by Side

Expression == (loose) === (strict) Why?
"5" vs. 5 true false String coerced to number
0 vs. false true false Boolean coerced to 0
null vs. undefined true false Special coercion rule
"" vs. 0 true false Empty string → 0
1 vs. 1 true true Same type, same value

How to Apply This Knowledge

1
Default to ===: always use strict equality unless you have a specific reason not to.
2
Check types first: if comparing user input (always a string) to a number, explicitly convert with Number() or parseInt().
3
Use == for null checks only: value == null catches both null and undefined in one check.
4
Read error messages: if a comparison produces an unexpected result, ask: "Are these the same type?"
5
Use typeof: when debugging, typeof x reveals the type of any value.

Example 1: String vs Number

let userInput = "100"; // from a text field
let target = 100;

console.log(userInput == target);  true
// "100" is coerced to 100, then 100 == 100

console.log(userInput === target); false
// string !== number, no coercion, immediate false

Example 2: Boolean Coercion Trap

// Does "0" equal false?
console.log("0" == false); true

// Step by step coercion:
// 1. false → 0 (boolean to number)
// 2. "0" → 0 (string to number)
// 3. 0 == 0 → true

// But with strict equality:
console.log("0" === false); false
// string !== boolean, done

Example 3: null vs undefined

console.log(null == undefined); true
console.log(null === undefined); false

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

The Weird Parts of ==

Here are some results that surprise even experienced developers. All of these use ==:

"" == 0 // true (empty string → 0)
" " == 0 // true (whitespace string → 0)
"" == false // true (both become 0)
"0" == false // true (both become 0)
"" == "0" // false (same type, different values!)

// So: "" == false, "0" == false, but "" != "0"
// Transitivity is BROKEN with ==

Truthy vs Falsy: Related but Different

Coercion also happens in if statements. JavaScript has exactly 6 falsy values:

Falsy Values
false
0
""
null
undefined
NaN
Truthy (everything else)
"0"  (non-empty string!)
"false"
[]  (empty array!)
{}  (empty object!)
42, -1

Common Mistakes

✕ Comparing user input with ==
input == 5 may be true even when input is the string "5". Convert explicitly with Number(input) first.
✕ Checking for null with ===
Using x === null || x === undefined is verbose. The shorthand x == null safely catches both.
✕ Assuming truthiness means == true
"hello" == true is false (string "hello" → NaN, true → 1, NaN != 1). Don't test truthiness with ==.

Summary: When to Use Which

Feature == (loose) === (strict)
Type coercion Yes, automatic None
Predictability Low: many edge cases High: type + value must match
Performance Slightly slower (coercion steps) Slightly faster (no coercion)
Best for null/undefined checks only Everything else
Industry standard Avoided in most style guides Recommended default

Question 1 of 5

What does "3" === 3 evaluate to?

Question 2 of 5

What does null == undefined evaluate to?

Question 3 of 5

Which statement best explains why "0" == false is true?

Question 4 of 5

A developer writes if (x == null) to check if a variable is null or undefined. Is this a good practice?

Question 5 of 5

What is the result of "" == false and "" === false?