JavaScript data types
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:
1const name = 'John';2const greeting = 'Hello';3const template = `Hi ${name}`;
Number — integers and decimals, including special values:
1const age = 30;2const price = 9.99;3const result = Infinity;4const 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:
1const huge = 9007199254740991n;2const 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.
Boolean — true or false:
1const isActive = true;2const isDeleted = false;
Undefined — a variable that has been declared but not assigned a value:
1let x;2console.log(x); // undefined
Null — an intentional "no value." You use it to explicitly say "this has nothing":
1let user = null; // we know there is no user
Symbol — a unique identifier, mostly used for object property keys:
1const id = Symbol('id');2const anotherId = Symbol('id');3console.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.
1// Regular object2const user = { name: 'John', age: 30 };34// Array (a special kind of object)5const numbers = [1, 2, 3];67// Function (also an object)8function greet() {}910// Date11const 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.
1// Primitives — independent copies2let a = 10;3let b = a;4b = 20;5console.log(a); // 10 — not affected67// Objects — shared reference8let obj1 = { name: 'John' };9let obj2 = obj1;10obj2.name = 'Jane';11console.log(obj1.name); // "Jane" — both point to the same object
typeof operator
You can check a value's type using typeof:
1typeof 'hello'; // "string"2typeof 42; // "number"3typeof true; // "boolean"4typeof undefined; // "undefined"5typeof Symbol(); // "symbol"6typeof 42n; // "bigint"7typeof {}; // "object"8typeof []; // "object" — arrays are objects9typeof null; // "object" — this is a known bug in JavaScript10typeof 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:
1'5' + 3; // "53" — number becomes string2'5' - 3; // 2 — string becomes number3true + 1; // 2 — true becomes 14false + 1; // 1 — false becomes 05null + 1; // 1 — null becomes 06undefined + 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.