Types and Values
Know the primitive types, how equality and truthiness work, and the handful of quirks every interviewer asks about.
Key points
- 1
There are seven primitive types (string, number, bigint, boolean, undefined, symbol, null); everything else is an object, including arrays and functions.
- 2
===compares without type conversion;==converts first. Use===by default, exceptx == nullas shorthand for null-or-undefined. - 3
The falsy values are
false,0,-0,0n,"",null,undefinedandNaN. Every object is truthy, even[]and{}. - 4
typeof nullis"object"(a historical bug) andNaN !== NaN. Test withvalue === nullandNumber.isNaN(value). - 5
Numbers are IEEE 754 doubles, so
0.1 + 0.2 !== 0.3. Compare with a tolerance, or work in integers (such as cents).
Common traps
The global
isNaN('abc')is true because it coerces;Number.isNaN('abc')is false.Object.is(0, -0)is false even though0 === -0is true.
Test yourself on Types and Values
Ten questions, with the answer and explanation after each one.