JavaScript · Chapter 11 of 55

JavaScript Data Types

JavaScript has primitive types — `string`, `number`, `boolean`, `undefined`, `null`, `symbol`, `bigint` — and one composite type, `object` (which includes arrays and functions).

JavaScript is dynamically typed: a variable's type is determined at runtime and can change if reassigned to a different kind of value.

Primitives

Primitives are immutable and compared by value. `typeof` reveals a value's type, though `typeof null` famously returns 'object' due to a historical bug.

Objects

Objects, arrays, and functions are all technically objects in JS. They are compared by reference, not by value.

Example 1 (javascript)
console.log(typeof "hi");
console.log(typeof 42);
console.log(typeof true);
console.log(typeof undefined);
Output
string
number
boolean
undefined

typeof reports the primitive type of a value.

Example 2 (javascript)
let arr = [1,2,3];
console.log(typeof arr, Array.isArray(arr));
Output
object true

Arrays report as 'object' via typeof, so use Array.isArray to check specifically.

Key points

  • Primitives: string, number, boolean, undefined, null, symbol, bigint.
  • Objects (including arrays, functions) are the only composite type.
  • `typeof null` returns 'object' — a known quirk.
  • JavaScript is dynamically typed.
💡 Note: Use Array.isArray() rather than typeof to reliably check for arrays.

📝 Quick Quiz

1. What does `typeof 42` return?

2. What does `typeof null` famously return?

3. Which checks if a value is an array?