Data Types & Type Checking

Difficulty: Beginner

Question

What are the data types in JavaScript, and how do you reliably check the type of a value?

Answer

JavaScript has eight data types: seven primitives (string, number, bigint, boolean, undefined, null, symbol) and object, which covers arrays, functions, dates, maps and everything else. Primitives are immutable and compared by value, while objects are compared by reference. For type checks, use typeof for primitives and functions, Array.isArray for arrays, === null for null, and instanceof or Object.prototype.toString for specific object kinds.

The seven primitives

Anything that is not a primitive is an object. Functions are callable objects, arrays are objects with integer keys and a length, and Date, Map, Set, RegExp and Promise are all objects.

Value vs reference semantics

When you assign or pass a primitive, the value is copied. When you assign or pass an object, the reference is copied, so both names point at the same object. That is why mutating an array inside a function changes the caller's array, and why {} === {} is false: two different objects never compare equal, even with identical contents.

What typeof returns, including the quirks

Valuetypeof result
'hi'"string"
42, NaN"number"
10n"bigint"
true"boolean"
undefined"undefined"
Symbol()"symbol"
null"object" (a historical bug that can never be fixed)
[], {}, new Date()"object"
function () {}, classes"function"

So typeof alone cannot tell an array from an object, or null from an object.

Reliable checks for each case

Where this bites in real projects

Money is a classic example. Storing a rupee amount like 199.99 as a float invites rounding errors, so payment systems such as Razorpay's APIs take amounts as integers in paise (19999). Large IDs from databases (64-bit integers) can exceed the safe integer range and silently lose precision when parsed as number; send them as strings or parse them as BigInt.

typeof, and what happens when types meet. Pick any two values and an operator under Tweak. Each operand shows its typeof.
  1. Seven primitives: string, number, bigint, boolean, undefined, symbol and null; everything else is an object.
  2. typeof null is "object": A historical bug kept for compatibility. Check for null with === null.
  3. Arrays are objects: typeof [] is "object"; use Array.isArray().
  4. null == 0 is false: null and undefined only loosely equal each other, so no number conversion happens.

Code examples

typeof and its quirks

console.log(typeof 'text');       // string
console.log(typeof 3.14);         // number
console.log(typeof NaN);          // number
console.log(typeof 9007199254740993n); // bigint
console.log(typeof Symbol('id')); // symbol
console.log(typeof undefined);    // undefined
console.log(typeof null);         // object  (legacy bug)
console.log(typeof []);           // object
console.log(typeof class A {});   // function

typeof is perfect for primitives and functions, but reports object for null, arrays and every other object type.

A reliable type helper

function getType(value) {
  if (value === null) return 'null';
  if (Array.isArray(value)) return 'array';
  const t = typeof value;
  if (t !== 'object') return t;
  // e.g. "[object Date]" -> "date"
  return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
}

console.log(getType(null));        // null
console.log(getType([1, 2]));      // array
console.log(getType(new Date()));  // date
console.log(getType(new Map()));   // map
console.log(getType(/upi/));       // regexp
console.log(getType(42));          // number

Handle null and arrays explicitly, use typeof for primitives, and fall back to the internal tag from Object.prototype.toString for specific object kinds.

Value vs reference copying

let a = 10;
let b = a;
b++;
console.log(a, b); // 10 11

const cart1 = ['milk'];
const cart2 = cart1;      // same array
cart2.push('bread');
console.log(cart1);       // [ 'milk', 'bread' ]

console.log([1] === [1]); // false: different objects
console.log(Number.MAX_SAFE_INTEGER + 2); // 9007199254740992 (precision lost)

Primitives are copied by value, objects share a reference. The last line shows why large integers need BigInt or strings.

Key points

Common follow-up questions

Interview tips

Concepts covered

Primitive Types, Reference Types, typeof, instanceof, Array.isArray, BigInt

More JavaScript Interview questions