Data Types, Strict Equality & Nullish Coalescing
Understand JavaScript's primitive types, why === beats ==, how truthy/falsy values work, and how ?? sets safe defaults.
What you'll learn
- Recognize primitive types: string, number, boolean, null, undefined, bigint, symbol
- Always prefer strict equality (===) over loose equality (==)
- Use nullish coalescing (??) to handle default values safely without breaking 0 or empty strings
JavaScript values fall into primitives (string, number, boolean, null, undefined, symbol, bigint) and objects (plain objects, arrays, functions, dates).
Always Use Strict Equality (===)
Loose equality (==) silently coerces types ("5" == 5 is true, "" == 0 is true), causing subtle bugs. Strict equality (===) checks both type and value.
const score = 0;
const display = score ?? 100; // 0!
if (score === 0) { ... } const score = 0;
const display = score || 100; // 100 (bug!)
if ("0" == 0) { ... } // true! Use ?? when 0, false, or '' are valid values and you only want a fallback for null or undefined.
Fill-in-the-Blank Exercise
Strict Comparison & Safe Default
Check strict equality and fall back to 'Guest' only when username is null or undefined.
const isAdmin = role 'admin';
const label = username 'Guest';Try It Yourself
No console output yet. Try calling console.log(...) in JS.
Check Your Understanding
1. What does `0 ?? 42` evaluate to in JavaScript?
Finished this lesson?
Your progress is saved locally in your browser, no account needed.