JavaScript data types

JavaScript

The short answer

JavaScript has eight data types. Seven of them are primitives: string, number, bigint, boolean, undefined, null, and symbol. The eighth is object, which includes arrays, functions, dates, and regular objects.

Primitive types

Primitives are the simplest values. They are immutable — you cannot change them. When you assign a primitive to a new variable, it copies the value.

String — text data, wrapped in quotes:

const name = 'John';
const greeting = 'Hello';
const template = `Hi ${name}`;

Number — integers and decimals, including special values:

const age = 30;
const price = 9.99;
const result = Infinity;
const invalid = NaN; // "Not a Number"

JavaScript uses a single number type for both integers and floating-point numbers. There is no separate int or float type.

BigInt — for numbers larger than what number can handle:

const huge = 9007199254740991n;
const alsoHuge = BigInt('9007199254740991');

You add n at the end to make it a BigInt. Regular numbers lose precision above 2^53 - 1, but BigInt can handle any size.

Booleantrue or false:

const isActive = true;
const isDeleted = false;

Undefined — a variable that has been declared but not assigned a value:

let x;
console.log(x); // undefined

Null — an intentional "no value." You use it to explicitly say "this has nothing":

let user = null; // we know there is no user

Symbol — a unique identifier, mostly used for object property keys:

const id = Symbol('id');
const anotherId = Symbol('id');
console.log(id === anotherId); // false — every symbol is unique

Object type

Everything that is not a primitive is an object. Objects are collections of key-value pairs, and they are stored by reference.

// Regular object
const user = { name: 'John', age: 30 };
// Array (a special kind of object)
const numbers = [1, 2, 3];
// Function (also an object)
function greet() {}
// Date
const now = new Date();

The key difference between primitives and objects is how they are stored. Primitives are stored by value — copying creates an independent copy. Objects are stored by reference — copying only copies the pointer.

// Primitives — independent copies
let a = 10;
let b = a;
b = 20;
console.log(a); // 10 — not affected
// Objects — shared reference
let obj1 = { name: 'John' };
let obj2 = obj1;
obj2.name = 'Jane';
console.log(obj1.name); // "Jane" — both point to the same object

typeof operator

You can check a value's type using typeof:

typeof 'hello'; // "string"
typeof 42; // "number"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof Symbol(); // "symbol"
typeof 42n; // "bigint"
typeof {}; // "object"
typeof []; // "object" — arrays are objects
typeof null; // "object" — this is a known bug in JavaScript
typeof function () {}; // "function"

Common Pitfalls

There are two well-known quirks with typeof. First, typeof null returns "object" instead of "null". This is a bug from the very first version of JavaScript that was never fixed. Second, typeof [] returns "object" because arrays are technically objects. To check for an array, use Array.isArray([]) instead.

Type coercion

JavaScript automatically converts types in certain situations, which can lead to surprising results:

'5' + 3; // "53" — number becomes string
'5' - 3; // 2 — string becomes number
true + 1; // 2 — true becomes 1
false + 1; // 1 — false becomes 0
null + 1; // 1 — null becomes 0
undefined + 1; // NaN

The + operator prefers strings (it concatenates), while -, *, / prefer numbers (they convert to numbers). This is why === (strict equality) is preferred over == (loose equality) — strict equality does not do type coercion.

Interview Tip

When answering this question, list all eight types and briefly explain each. Make sure to mention the typeof null bug and the difference between primitives (by value) and objects (by reference). If the interviewer asks follow-up questions, knowing about type coercion and how === vs == work will impress them.

Why interviewers ask this

This is a fundamental JavaScript question. Interviewers use it to check your baseline knowledge. They want to see if you know all eight types, if you understand the difference between primitives and objects, and if you are aware of the quirks like typeof null. It is also a good lead-in to deeper questions about type coercion, equality, and how references work.