Data types in JavaScript
JavaScript has eight basic data types, split into primitives (seven of them) and the reference, object type (one). A primitive is a value that is not an object and has no methods of its own, while an object is a container for data and functions that is passed by reference.
Theory
TL;DR
- There are exactly 8 types:
Number,BigInt,String,Boolean,Null,Undefined,SymbolandObject. - The first seven are primitives: stored by value, with no methods of their own.
Objectis the only reference type;Array,Function,Date,RegExp,MapandSetare flavours of it.- Every ordinary number is a 64-bit floating point number (IEEE 754), hence
Infinity,-InfinityandNaN. typeof nullreturns'object', an old language bug kept for backwards compatibility.
Quick example
const count = 42; // Number
const huge = 123456789012345678n; // BigInt
const name = 'Tim'; // String
const isAdmin = true; // Boolean
const nothing = null; // Null
let notAssigned; // Undefined
const id = Symbol('id'); // Symbol
const user = { name, [id]: 1 }; // Object
console.log(typeof count, typeof huge, typeof name); // number bigint string
console.log(typeof nothing, typeof notAssigned, typeof id); // object undefined symbolPrimitives versus objects
A primitive is a simple immutable value. When you assign it to another variable the value itself is copied, so the two variables live independently. An object, by contrast, sits in memory on its own and the variable holds only a reference to it, so the reference is copied, not the contents.
let a = 10;
let b = a;
b = 20;
console.log(a); // 10, the primitive was copied by value
const first = { n: 10 };
const second = first;
second.n = 20;
console.log(first.n); // 20, both variables look at the same objectPrimitives have no methods, yet 'abc'.toUpperCase() works: the engine creates a temporary wrapper object on the fly, takes the method from it and throws the wrapper away immediately.
Numeric types: Number and BigInt
Number
Numbers, both integers and floating point ones.
let a = 42;
let b = 3.14;Details:
- There are special values:
Infinity,-Infinity,NaN(Not-a-Number). - Every number in JavaScript is a 64-bit floating point number (IEEE 754), which is why
0.1 + 0.2 !== 0.3. - The safe integer range is capped by
Number.MAX_SAFE_INTEGER(2 to the power of 53 minus 1).
BigInt
Used for very large integers that fall outside the range of Number.
let big = 123456789012345678901234567890n;The
nsuffix is added. It lets you work precisely with integers of any length.
BigInt does not mix with Number in arithmetic: 1n + 1 throws a TypeError, an explicit conversion is required first.
Text and logic: String and Boolean
String
Lines of text in quotes: single, double or template ones.
let name = 'Tim';
let message = `Hello, ${name}!`;Strings are immutable: a "change" creates a new string and leaves the old one untouched.
let s = 'abc';
s[0] = 'X';
console.log(s); // 'abc', the assignment is simply ignoredBoolean
Logical values: true or false.
let isAdmin = true;
let isLoggedIn = false;Used in conditions, checks and logical expressions. The only falsy values are
false,0,-0,0n,'',null,undefinedandNaN; everything else is truthy.
Empty values and Symbol
Null
A special value meaning "nothing" or "empty", assigned deliberately by the programmer.
let user = null;
typeof nullreturns'object', a historical JavaScript bug that was never fixed.
Undefined
A variable is declared but holds no value.
let x;
console.log(x); // undefinedIt is assigned automatically when no value is given: it is also what you read from a missing property and what an omitted parameter holds.
Symbol
A unique, immutable value, often used as an object key.
let id = Symbol('id');
let obj = { [id]: 123 };
console.log(Symbol('id') === Symbol('id')); // falseEvery Symbol is unique even when the description is the same. Symbol keys do not show up in
Object.keys()orJSON.stringify(), which makes them suitable for internal fields.
Object: the reference type
Holds collections of data and functions. Passed by reference, not by value.
let user = { name: 'Tim', age: 25 };
let arr = [1, 2, 3];
let func = function () {};Flavours of object:
Object, the base type.Array, an array.Function, a function.Date,RegExp,Map,Setand so on.
| Data type | Example | Description |
|---|---|---|
| Number | 42, 3.14, NaN | Numbers |
| BigInt | 123n | Large integers |
| String | 'Hello', `Hi ${name}` | Strings |
| Boolean | true, false | Logical type |
| Null | null | Absence of a value |
| Undefined | undefined | Not defined |
| Symbol | Symbol('id') | Unique identifier |
| Object | {}, [], function(){} | Collections and structures |
Common mistakes
- Calling an array a separate type.
typeof [1, 2]gives'object'; you needArray.isArray()to tell an array apart. - Mixing up
nullandundefined.undefinedmeans "the engine has not put a value here yet",nullis emptiness the developer set on purpose. - Forgetting that
typeof null === 'object'and building an object check on it: an extravalue !== nullcondition is required. - Comparing
NaNwith itself.NaN === NaNisfalse, useNumber.isNaN()instead. - Mixing
BigIntandNumberin one expression and getting aTypeError. - Treating a string as mutable and assigning a character by index; such a string has to be rebuilt from scratch.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.