Study notes · 15% of the exam

Types and Values

Know the primitive types, how equality and truthiness work, and the handful of quirks every interviewer asks about.

Key points

  1. 1

    There are seven primitive types (string, number, bigint, boolean, undefined, symbol, null); everything else is an object, including arrays and functions.

  2. 2

    === compares without type conversion; == converts first. Use === by default, except x == null as shorthand for null-or-undefined.

  3. 3

    The falsy values are false, 0, -0, 0n, "", null, undefined and NaN. Every object is truthy, even [] and {}.

  4. 4

    typeof null is "object" (a historical bug) and NaN !== NaN. Test with value === null and Number.isNaN(value).

  5. 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 though 0 === -0 is true.

Test yourself on Types and Values

Ten questions, with the answer and explanation after each one.