Data Types
Every value in JavaScript has a type, which decides what the value is and what you can do with it. JavaScript has seven primitive types for simple values and one object type for everything more complex. Knowing the difference explains many of the language's surprises.
The primitive types
| Type | What it holds | Examples |
|---|---|---|
| string | Text | "Nairobi", 'M-Pesa' |
| number | Whole numbers and decimals (one type for both) | 42, 3.14, -7 |
| bigint | Integers too large for number | 9007199254740993n |
| boolean | Yes or no | true, false |
| undefined | A variable that hasn't been given a value | undefined |
| null | "Intentionally empty" | null |
| symbol | A unique identifier, used in advanced code | Symbol("id") |
Primitives are immutable: you can't change the value itself, only replace it with a new one. Methods like "hello".toUpperCase() return a brand-new string and leave the original untouched.
Everything that isn't a primitive is an object: plain objects like { name: "Amina" }, arrays, functions, dates and more.
Checking types with typeof
The typeof operator returns the type of a value as a string:
console.log(typeof "Nairobi");
console.log(typeof 42);
console.log(typeof 3.14);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof null);
console.log(typeof [1, 2, 3]);
console.log(typeof { name: "Amina" });
console.log(typeof function () {});
console.log(typeof 10n);
Two results deserve attention. typeof null is "object", which is a famous bug from the first version of JavaScript that can never be fixed without breaking old websites; null is really its own primitive type. And arrays report "object" because arrays are objects. To test for an array, use Array.isArray(value), and to test for null, compare directly: value === null. Functions are objects too, but typeof gives them their own answer, "function".
undefined vs null
Both mean "no value", but they are used differently. JavaScript itself produces undefined: for a declared variable with no value, a missing object property, or a function that doesn't return anything. You, the programmer, write null when you want to say "deliberately empty".
let phone;
console.log(phone);
const profile = { name: "Juma", middleName: null };
console.log(profile.middleName);
console.log(profile.email);
Here Juma is known to have no middle name (null), whereas nobody has recorded an email at all (undefined).
Dynamic typing
JavaScript is dynamically typed: types belong to values, not to variables, so one let variable can hold a number now and a string later. That flexibility is convenient but means mistakes show up only when the code runs. JavaScript will also convert types automatically in some situations, for example "5" + 1 gives the string "51". The Operators page explains these conversions, called type coercion.
The special value NaN ("Not a Number") appears when a calculation fails, such as "abc" * 2. Confusingly, typeof NaN is "number". See Numbers & Math.
Primitives are copied, objects are shared
This is the single most important idea on this page. When you assign a primitive to another variable, the value is copied. When you assign an object, only a reference (an arrow pointing to the object) is copied, so both names point at the same object.
let a = 10;
let b = a; // copies the value 10
b = 20;
console.log(a, b);
const original = { name: "Amina", age: 20 };
const copy = original; // copies the reference only
copy.age = 21;
console.log(original.age);
console.log(original === copy);
b can't affect a. But original and copy are two arrows to the same object, so a change made through one is visible through the other.To get a real, separate copy of an object, use the spread syntax: const clone = { ...original };. Changing clone.age then leaves original alone, and original === clone is false because they are two different objects. (Spread makes a shallow copy; the Objects page explains what that means.)
=== on objects compares references, not contents. { a: 1 } === { a: 1 } is false because they are two different objects that happen to look the same. Likewise [] === [] is false.
Key points
- The seven primitives are string, number, bigint, boolean, undefined, null and symbol; everything else is an object.
typeofreturns a type name as a string, buttypeof nullis"object"and arrays are"object"too.undefinedmeans "not set yet";nullmeans "deliberately empty".- Assigning a primitive copies the value; assigning an object copies a reference to the same object.
Tip: use the ← and → keys to move between pages.
