Values, Types, and Coercion
The primitive types JavaScript has, what `typeof` really tells you, how implicit vs explicit coercion works, and truthy/falsy and `NaN` quirks.
Values, Types, and Coercion
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- JavaScript has a small set of primitive types — string, number, boolean, undefined, null, symbol, and bigint — plus one compound type, object; arrays, functions, and dates are all objects underneath.
- `typeof` tells you what kind of value you're holding, with one famous wrinkle: `typeof null` returns `"object"`, a bug preserved since JavaScript's first release because fixing it now would break code across the web.
- Coercion is JavaScript converting a value from one type to another to make an operation work — explicit when you call `String(x)` or `Number(x)` yourself, implicit when an operator like `+` or `==` does the conversion on your behalf without being asked.
- Every value is truthy except a short, fixed list of falsy ones — `false`, `0`, `-0`, `0n`, `""`, `null`, `undefined`, and `NaN` — worth memorizing once instead of guessing per value.
- `NaN` is the one value in JavaScript that isn't equal to itself — `NaN === NaN` evaluates to `false` — so you check for it with `Number.isNaN()`, never with an equality operator.
Why a language needs to care about types at all
Every value you touch in a program is one kind of thing or another — a
number you’re about to do arithmetic with, a string you’re about to
display, a boolean deciding which branch to take. Most of the time that’s
invisible; you write age + 1 and it just works. The trouble starts the
moment two different types meet in one operation — adding a number to a
string, comparing a string typed into a form against a number stored in a
database — because now the language has to decide what to do, and
JavaScript’s answer is often “convert one of them and proceed” rather than
“refuse.” Understanding when and how it converts things is the
difference between coercion being a convenience and it being the source of
a bug you’ll stare at for twenty minutes.
The types you’re actually working with
Strip away syntax and JavaScript has a short list of primitive types:
string, number, boolean, undefined, null, symbol, and bigint.
Everything else — arrays, functions, dates, regular expressions, the
objects you build with {} — falls under one umbrella type, object.
That’s a smaller set than it might feel like day to day, and the split
matters beyond trivia: primitives are compared and copied by value, while
objects are compared and copied by reference, which is a distinction
you’ll run into constantly once you start passing data between functions.
typeof is how you ask JavaScript what type a value is at runtime, and
it’s mostly reliable — typeof 5 is "number", typeof "hi" is
"string". The one well-known exception is typeof null, which returns
"object" instead of something like "null". That’s not a design choice
anyone would make on purpose; it’s a bug from JavaScript’s first release
that’s stuck around for decades because fixing it would silently change
behavior on an unknowable number of existing websites.
Coercion: conversion you asked for, and conversion you didn’t
Explicit coercion is you calling a conversion function directly:
Number("42"), String(42), Boolean(0). There’s no ambiguity about
what’s happening or why.
Implicit coercion is JavaScript converting a type on its own, as a
side effect of some other operation, because the operator you used needs
operands of a particular type to make sense. "5" + 2 doesn’t throw an
error for mixing a string and a number — it converts 2 to "2" and
concatenates, giving "52". Swap the operator to -, and "5" - 2
converts "5" to the number 5 instead, giving 3, because subtraction
has no string meaning to fall back on the way + does. The video walks
through exactly this kind of operator-by-operator behavior and the bugs it
causes in real code, which is why it’s the pick for this lesson over a
page that just lists the conversion rules in the abstract — seeing the
same operator produce two different kinds of surprises back to back is
what actually makes the rule stick.
Truthy, falsy, and the list worth memorizing
Anywhere JavaScript expects a boolean — an if condition, a && chain —
a non-boolean value gets coerced to one. Rather than reason this out each
time, it’s faster to just know the complete list of falsy values: false,
0, -0, 0n, "" (empty string), null, undefined, and NaN.
Literally everything else is truthy, including values that feel like they
should be empty — [] and {} are both truthy, because they’re objects,
and every object is truthy regardless of what’s inside it.
NaN: the value that isn’t equal to itself
NaN stands for “not a number” and shows up when a numeric operation
doesn’t have a sensible numeric result — 0 / 0, or Number("hello"). Its
strangest property is that it’s never equal to anything, including
another NaN: NaN === NaN is false. That’s not a JavaScript quirk so
much as inherited behavior from the floating-point standard numbers follow
— but it does mean === NaN is never the right way to check for it. Use
Number.isNaN(value) instead, which checks the actual bit pattern rather
than relying on equality.
Where this fits
This lesson is foundational on purpose — equality and comparison
builds directly on the coercion rules introduced here, since ==’s
behavior is coercion rules applied to a comparison. Functions, objects,
and nearly everything downstream in this roadmap assume you already know
the difference between a primitive and an object, and that a value being
“empty-looking” doesn’t make it falsy. Get comfortable with this list now
and a lot of JavaScript’s reputation for being “weird” turns out to just
be these few rules, applied consistently.
Check yourself
What does `typeof null` return, and why does that answer surprise most people?
It returns `"object"`, even though `null` is a primitive, not an object. It's a mistake baked into the very first version of JavaScript in 1995 — by the time anyone wanted to fix it, enough code on the web quietly depended on that behavior that changing it would break existing sites, so it was left in place permanently.
What's the difference between implicit and explicit coercion, with one example of each?
Explicit coercion is you asking for a conversion directly, like `Number("5")` turning the string `"5"` into the number `5`. Implicit coercion is the engine converting a type on its own as a side effect of some other operation — `"5" + 2` implicitly converts the number `2` to the string `"2"` before concatenating, producing `"52"`, not `7`.
List every falsy value in JavaScript. What does that imply about an empty array or empty object in a boolean context?
`false`, `0`, `-0`, `0n`, `""`, `null`, `undefined`, and `NaN` — that's the complete list. Everything else is truthy, which includes an empty array `[]` and an empty object `{}`. Both look "empty" and feel like they should be falsy, but they're objects, and every object is truthy regardless of what it contains.
Why is `NaN === NaN` false, and how do you actually check whether a value is `NaN`?
`NaN` means "not a number produced by a failed or undefined numeric operation," and by design no `NaN` is considered equal to any value, including another `NaN` — this is part of the IEEE 754 floating-point spec JavaScript's numbers follow, not a JavaScript-specific quirk. Use `Number.isNaN(value)` to check for it reliably; comparing with `===` or `==` against `NaN` always returns `false`.
What's the practical difference between comparing two primitives and comparing two objects for equality?
Primitives are compared by value — two strings or numbers with identical content are equal regardless of where they came from. Objects (including arrays and functions) are compared by reference — two objects are only equal if they're literally the same object in memory, so `{ a: 1 } === { a: 1 }` is `false` even though their contents match exactly.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.