JavaScript has eight main data types: seven primitives (string, number, bigint, boolean, undefined, symbol, null) and one non-primitive type (object). Understanding which values are primitive vs object helps with comparisons, copying, and mutation behavior. This affects state updates, copying, and performance; test typeof null and array detection.
What are the various data types in JavaScript?
The Core Idea\n\nJavaScript values fall into two main categories:\n- Primitive types — stored directly, immutable, and compared by value.\n- Non-primitive type — objects, which are mutable and compared by reference.\n\nThere are 8 total data types in JavaScript (as of ES2020).
Category | Data Type | Description | Example |
|---|---|---|---|
Primitive | Number | Represents both integers and floating-point numbers. |
|
Primitive | String | Represents text data enclosed in quotes. |
|
Primitive | Boolean | Represents true/false values. |
|
Primitive | Undefined | A variable that has been declared but not assigned a value. |
|
Primitive | Null | An intentional empty value (represents 'nothing'). |
|
Primitive | Symbol | A unique and immutable value often used as object keys. |
|
Primitive | BigInt | Used for integers larger than |
|
Non-Primitive | Object | Used to store collections of data and more complex entities. |
|
Primitives Are Immutable\n\nPrimitive values can’t be changed directly — any modification creates a new value instead.\n\n``javascript\nlet str = 'Hi';\nstr[0] = 'Y'; // ❌ has no effect\nconsole.log(str); // 'Hi'\n``
Objects Are Mutable\n\nObjects, arrays, and functions can be modified because they are stored by reference.
const user = { name: 'Alice' };\nconst ref = user;\nref.name = 'Bob';\nconsole.log(user.name); // 'Bob' (same reference)
Common Confusion\n\ntypeof null returns 'object' — this is a well-known JavaScript quirk kept for backward compatibility.
console.log(typeof null); // 'object'
Practical scenario
An API returns mixed values; you need to detect primitives vs objects before cloning or updating state.
Common pitfalls
- Assuming
typeof nullis "object" for valid checks. - Treating arrays as plain objects without
Array.isArray. - Mutating objects when you meant to copy.
Primitives are predictable, objects are flexible but mutable. Test
typeof, Array.isArray, and reference equality.Think of primitives as single paper notes — once written, they can’t be changed. Objects are like folders — you can add, remove, or edit their contents anytime.
- Primitive types (7): Number, String, Boolean, Undefined, Null, Symbol, BigInt.
- Non-primitive type (1): Object.
Everything else in JavaScript (arrays, functions, dates, regex, etc.) is an object.
Use the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.