Hiprup

What are the different data types in JavaScript?

JavaScript has 8 data types: 7 primitive types plus 1 reference type (object).

Primitives are immutable and compared by value:

  • String — text such as "hello".

  • Number — all integers and decimals (a single numeric type).

  • BigInt — whole numbers too large for Number to hold safely.

  • Boolean — true or false.

  • Undefined — a variable declared but not yet assigned.

  • Null — an intentional "empty" value.

  • Symbol — a unique, immutable identifier.

Reference type — the Object, which covers arrays, functions, dates and more. Objects are stored and compared by reference, not value.

Classic gotcha: typeof null returns "object" — a long-standing language bug, not proof that null is an object.

// Primitive types
console.log(typeof 'hello');     // 'string'
console.log(typeof 42);          // 'number'
console.log(typeof 42n);         // 'bigint'
console.log(typeof true);        // 'boolean'
console.log(typeof undefined);   // 'undefined'
console.log(typeof Symbol('id')); // 'symbol'
console.log(typeof null);        // 'object' (bug!)

// Non-primitive
console.log(typeof {});          // 'object'
console.log(typeof []);          // 'object' (use Array.isArray)
console.log(typeof function(){}); // 'function'

// Checking types properly
console.log(Array.isArray([]));     // true
console.log(null === null);          // true (check null with ===)
console.log(Number.isNaN(NaN));      // true (not global isNaN)
console.log(Number.isFinite(42));    // true

typeof works for most types but has gotchas: null returns 'object' and arrays return 'object'. Use Array.isArray for arrays, === null for null, Number.isNaN for NaN (global isNaN coerces first), and Number.isFinite for finite numbers.

Name all 7 primitives + object. The typeof null === 'object' bug is the #1 JS gotcha question.

Know the proper type-checking methods: Array.isArray, === null, Number.isNaN.

What are the different data types in JavaScript? | Hiprup