Lesson 3 of 28
In progress
Types, coercion and the truth about typeof
Learn JavaScript's primitive types, when values convert themselves, and how to compare without surprises.
JavaScript is dynamically typed: a variable can hold a number now and a string later. That flexibility is convenient until "5" + 1 gives "51" and a total silently becomes text.
What you will learn
- Name the primitive types and check them with
typeof. - Predict when JavaScript converts a value automatically.
- Compare values with
===and convert deliberately.
Seven primitives
The primitive types are string, number, boolean, null, undefined, symbol and bigint. Everything else is an object, including arrays and functions.
Editable. Runs in your browser.
Press Run to see the output.
typeof null returns "object", a bug preserved for compatibility. typeof [] also returns "object", because arrays are objects. To test for an array, use Array.isArray(value); to test for null, use value === null.
undefined means "not assigned"; null means "deliberately empty". They are different values, and mixing them up produces conditions that are almost right.
Numbers are all floats
There is one number type, a 64-bit float. Integers work exactly up to 2^53, and 0.1 + 0.2 is not 0.3.
Editable. Runs in your browser.
Press Run to see the output.
For money, work in the smallest unit (paise, cents) as integers, or use a decimal library. Formatting for display is a separate step, which toFixed(2) handles, though it returns a string.
NaN means "not a number" and is the result of invalid arithmetic. It is not equal to itself, so test with Number.isNaN(value), not value === NaN.
Coercion happens in two directions
Explicit conversion is something you ask for:
Editable. Runs in your browser.
Press Run to see the output.
Implicit coercion is JavaScript deciding for you. The + operator is the main culprit: if either side is a string, it concatenates.
Editable. Runs in your browser.
Press Run to see the output.
The first prints "51", the second 4, the third 2. Subtraction has no string meaning, so it converts both sides to numbers; true becomes 1. This inconsistency is why explicit conversion is worth the extra characters.
A common real bug: reading a value from an input gives a string, and quantity * price may happen to work because * coerces, while quantity + price concatenates. Convert at the boundary with Number(value).
Truthiness
These are falsy: false, 0, -0, 0n, "", null, undefined and NaN. Everything else is truthy, including "0", [] and {}.
Editable. Runs in your browser.
Press Run to see the output.
An empty array is truthy, which is why if (items) does not check whether the list has items. Use items.length > 0 when you mean that.
Strict equality
Use === and !==. They compare type and value. == converts types before comparing, producing rules almost nobody remembers correctly.
Editable. Runs in your browser.
Press Run to see the output.
The last line is true, which is one of the few places == is defensible. Even there, an explicit value == null check is often used to catch both null and undefined. If you use it, say so in a comment.
Objects compare by reference, not by content:
Editable. Runs in your browser.
Press Run to see the output.
Two separate objects with the same contents are not equal. That matters for arrays and for React-style state comparisons later.
if (value) fails for the number 0 and the empty string, which are perfectly valid values. When zero or empty text is meaningful, write if (value !== undefined && value !== null) or check the type explicitly, rather than relying on truthiness.
Predict the output of each line, then run it: "10" + 5, "10" - 5, Number("10") + 5, [] == false, Boolean("false"). Explain each result in one sentence.
Show solution
Editable. Runs in your browser.
Press Run to see the output.
"10" + 5 concatenates to "105" because one side is a string. "10" - 5 is 5 because subtraction coerces. Number("10") + 5 is 15. [] == false is true because both convert to 0 under loose equality. Boolean("false") is true because a non-empty string is truthy.
Recap
- Primitives are string, number, boolean, null, undefined, symbol and bigint; arrays and functions are objects.
- Use
===, convert at the boundary, and never trusttypeoffor null or arrays. - Falsy values include
0,"",null,undefinedandNaN;[]is truthy.
Next, you will combine values with operators and the string methods you use constantly.
Should I use parseInt or Number?
Why is NaN not equal to itself?
Progress is saved in this browser only. No account, no email.