What are the Data Types in JavaScript?
Learn the data types in JavaScript: the seven primitives, the object type, typeof quirks like null, and how value vs reference copying works, with examples.
Expected Interview Answer
JavaScript has two categories of data types: primitive types (string, number, bigint, boolean, undefined, null, and symbol) which hold single immutable values, and one non-primitive type, object, which includes arrays, functions, and dates.
The seven primitives are copied by value and are immutable, while objects are stored and copied by reference. The typeof operator reveals a value's type, though it has a famous quirk: typeof null returns 'object' for historical reasons. Understanding these types matters because coercion, equality, and copying behavior all depend on whether a value is a primitive or an object.
- Clarifies how values are stored and copied
- Explains coercion and equality behavior
- Helps predict typeof results
- Foundation for understanding mutability and references
- Prevents bugs when passing values into functions
AI Mentor Explanation
Primitive types are like individual runs on the scoreboard — each number, name, or true/false stands alone and copying it just writes the same figure elsewhere. Objects are like the full team sheet: a shared document that many people reference, so if one person edits the batting order everyone reading that same sheet sees the change, unlike a copied single score.
Step-by-Step Explanation
Step 1
Split into two groups
Separate values into primitives and the single non-primitive type, object.
Step 2
List the primitives
string, number, bigint, boolean, undefined, null, and symbol — seven immutable value types.
Step 3
Understand objects
Arrays, functions, dates and plain objects are all of type object, stored by reference.
Step 4
Use typeof
Apply typeof to inspect a value's type, remembering typeof null is the quirky 'object'.
Step 5
Value vs reference
Primitives copy by value; objects copy the reference, so shared objects reflect edits everywhere.
What Interviewer Expects
- Naming all seven primitive types
- Knowing object is the non-primitive type
- Awareness that typeof null returns 'object'
- Difference between value and reference copying
- Knowing arrays and functions are objects
Common Mistakes
- Forgetting bigint or symbol as primitives
- Calling arrays a separate primitive type
- Saying typeof null returns 'null'
- Thinking objects are copied by value like primitives
Best Answer (HR Friendly)
“JavaScript values come in two kinds: primitives like text, numbers, true/false, undefined, null, symbols and bigints, which are simple standalone values, and objects like arrays and functions, which can hold many values and are shared by reference. Knowing the difference helps predict how data is copied and compared.”
Code Example
const name = 'Ada'; // string
const age = 36; // number
const big = 9007199254740993n; // bigint
const active = true; // boolean
let nothing; // undefined
const empty = null; // null
const id = Symbol('id'); // symbol
const user = { name, age }; // object
const nums = [1, 2, 3]; // object (array)
console.log(typeof name); // 'string'
console.log(typeof big); // 'bigint'
console.log(typeof empty); // 'object' (historical quirk)
console.log(typeof nums); // 'object'
console.log(Array.isArray(nums)); // trueFollow-up Questions
- Why does typeof null return 'object'?
- What is the difference between undefined and null?
- How are primitives and objects copied differently?
- When would you use a symbol or a bigint?
- How can you reliably check if a value is an array?
MCQ Practice
1. How many primitive data types does modern JavaScript have?
The seven primitives are string, number, bigint, boolean, undefined, null, and symbol.
2. What does typeof null return?
Due to a long-standing bug kept for backward compatibility, typeof null returns 'object'.
3. Which of these is NOT a primitive type?
An array is an object (non-primitive); symbol, bigint, and boolean are all primitives.
Flash Cards
How many primitive types are there? — Seven: string, number, bigint, boolean, undefined, null, and symbol.
What is the non-primitive type? — object — which includes arrays, functions, and dates.
What does typeof null return? — 'object', a historical quirk kept for backward compatibility.
How are objects copied? — By reference, so a shared object reflects edits everywhere it is used.