# Data Types & Type Checking

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

Canonical: https://www.internhack.in/learn/interview/javascript-interview/js-data-types
Last updated: 2026-11-28

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

- `string`: UTF-16 text, immutable. Methods return new strings.
- `number`: a 64-bit IEEE 754 float. There is no separate integer type, which is why `0.1 + 0.2 !== 0.3` and why integers are only exact up to `Number.MAX_SAFE_INTEGER` (2^53 - 1). Special values include `NaN`, `Infinity` and `-0`.
- `bigint`: arbitrary-precision integers written as `10n`. You cannot mix `bigint` and `number` in arithmetic without converting.
- `boolean`: `true` or `false`.
- `undefined`: the value of a declared but unassigned variable, a missing property or a missing argument.
- `null`: an intentional "no value".
- `symbol`: a unique identifier, often used as non-colliding object keys (`Symbol.iterator`).

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

| Value | `typeof` 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

- Null: `value === null`. For null or undefined together, `value == null` is the one accepted use of loose equality.
- Arrays: `Array.isArray(value)`. It works across iframes and realms, where `instanceof Array` fails because each realm has its own `Array` constructor.
- NaN: `Number.isNaN(value)`, not the global `isNaN`, which coerces strings first (`isNaN('abc')` is `true`).
- Integers: `Number.isInteger` and `Number.isSafeInteger`.
- Class instances: `value instanceof Date`, which walks the prototype chain.
- Precise tag: `Object.prototype.toString.call(value)` returns strings like `[object Date]` or `[object Null]`.

### 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

- Eight types: seven primitives plus object
- typeof null is 'object'; check null with === null
- Use Array.isArray, not typeof or instanceof Array, for arrays
- Number.isNaN is safer than the global isNaN, which coerces
- number is a 64-bit float; integers are safe only up to 2^53 - 1, beyond that use BigInt

## Common follow-up questions

- Why does typeof null return 'object'?
- Why is 0.1 + 0.2 not equal to 0.3, and how would you handle money?
- When does instanceof give the wrong answer?
- How would you deep-compare two objects?

## Interview tips

- List all seven primitives including bigint and symbol; many candidates forget them
- Mention the typeof null bug and the Array.isArray fix before the interviewer asks
- Tie it to something practical, such as storing amounts in paise or IDs as strings

## Concepts covered

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

Previous: [Variables: var, let, const](https://www.internhack.in/learn/interview/javascript-interview/js-var-let-const) · Next: [Operators & Short-Circuit Evaluation](https://www.internhack.in/learn/interview/javascript-interview/js-operators-short-circuit) · [All of JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)

## More JavaScript Interview questions

- [String Methods & Template Literals](https://www.internhack.in/learn/interview/javascript-interview/js-string-methods)
- [Array Methods (map, filter, reduce)](https://www.internhack.in/learn/interview/javascript-interview/js-array-methods)
- [Destructuring & Spread/Rest](https://www.internhack.in/learn/interview/javascript-interview/js-destructuring-spread)
- [Error Handling (try/catch/finally)](https://www.internhack.in/learn/interview/javascript-interview/js-error-handling)
- [== vs === Equality Operators](https://www.internhack.in/learn/interview/javascript-interview/js-equality-operators)
- [Arrow Functions vs Regular Functions](https://www.internhack.in/learn/interview/javascript-interview/js-arrow-functions)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)
