Coding/Learn/javascript/type coercion and equality
JavaScript Foundational Course
01.Introduction to JavaScript & V8 Engine
02.Console Output & Program Flow
03.Variables & Data Types (let, const, typeof)
04.Template Literals & String Interpolation
05.Type Coercion & Strict Equality (=== vs ==)
06.Arithmetic, Math Object & Modulo
07.Conditionals & If / Else Branching
08.Logical Operators & Nullish Coalescing (??)
09.Ternary Operator & Switch Statements
10.While & Do-While Loops
11.Standard For Loops & Iteration
12.Loop Control (Break & Continue)
13.Functions, Arrow Syntax & Return Values
14.Default Parameters, Rest & Spread Syntax
15.Arrays, Indexing & Mutating Methods
16.Array Iterators (map, filter, reduce)
17.Objects, Key-Value Pairs & Destructuring
18.Scope, Lexical Environment & Closures
19.Asynchronous JavaScript (Promises & Async/Await)
20.Capstone: Interactive E-Commerce Cart & Ledger Engine
Course Certificate0%

Complete all lessons to earn

LearnType Coercion & Strict Equality (=== vs ==)
5 / 20 0%
Lesson 05 of 20
6 mins read

Type Coercion & Strict Equality (=== vs ==)

PreviousNext Lesson
In Plain English

JavaScript automatically converts types in certain operations (type coercion). Strict equality (===) compares both value and type without coercion, while loose equality (==) coerces operands before comparison.

The Mental Model

"Loose equality (==) is like a forgiving bouncer who lets you in if you look somewhat similar in poor lighting. Strict equality (===) is a biometric fingerprint scanner checking exact identity."

Deep Dive: How It Works

Strict Equality (===): Checks value AND type. 5 === "5" is false.

Loose Equality (==): Converts types before checking. 5 == "5" is true. (Avoid in modern code!).

The 8 Falsy Values in JavaScript: false, 0, -0, 0n, "", null, undefined, NaN. Everything else is truthy.

Syntax Blueprint

a === b ──▶ Strict equality (no coercion) | a !== b ──▶ Strict inequality

Always prefer === and !== over == and != to avoid unexpected type coercion bugs.

PointCore Rules to Remember

Point star
Strict Equality: Always use === and !== to ensure type safety.
Point star
Truthy & Falsy: Empty strings, 0, null, undefined, and NaN evaluate to false in boolean contexts.
Point star
Explicit Conversion: Use Number("42"), String(100), and Boolean(value) for safe, clear conversions.

Common Beginner Traps & How to Fix Them

Mistake:Using == with 0 or empty strings.

Why it happens: 0 == "" evaluates to true due to historic type conversion rules.

How to fix: Enforce === everywhere across your JavaScript codebase.

Pro Tip: Use the double-not operator (!!value) as a fast idiom to convert any value into its strict boolean representation.

Live Interactive Example

Hit Run Code to see it live
Comparing Equality and Truthiness
JavaScript ES2024
1
2
3
4
5
6
7
8
Output Console
Click "Run Code" to view the rendered output.
How it works: Demonstrates type checking and boolean conversions in JavaScript.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Perform explicit type conversion

  • PointGiven const input = "500";
  • PointConvert input to a number using Number(input) and add 100.
  • PointPrint "Converted Total: 600".
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates