Lesson 5 / 38

Data Types

Primitives, objects, and how to check a type.

Primitive types

number, string, boolean, undefined, null, symbol, and bigint — seven primitives. Everything else is an object.

typeof operator

typeof reports a value's type at runtime — handy for quick checks.

console.log(typeof 42);          // "number"
console.log(typeof "hi");        // "string"
console.log(typeof true);        // "boolean"
console.log(typeof undefined);   // "undefined"
console.log(typeof {});          // "object"
console.log(typeof null);        // "object" (a famous quirk)

null vs undefined

undefined means a variable was declared but never assigned. null is an intentional "no value" you assign yourself.

Quick check: What does `typeof null` return?

  • "null"
  • "object"
  • "undefined"
Answer

"object" — It's a long-standing JS bug kept for backward compatibility.