Skip to content
Kuza Ndoto Academy

Data Types

Beginner → IntermediateChapter 1 · Page 5 of 324 min readUpdated 15 Nov 2026

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

TypeWhat it holdsExamples
stringText"Nairobi", 'M-Pesa'
numberWhole numbers and decimals (one type for both)42, 3.14, -7
bigintIntegers too large for number9007199254740993n
booleanYes or notrue, false
undefinedA variable that hasn't been given a valueundefined
null"Intentionally empty"null
symbolA unique identifier, used in advanced codeSymbol("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);
string number number boolean undefined object object object function bigint

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);
undefined null undefined

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.

Note

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);
10 20 21 true
Primitives: the value is copied a 10 b 20 two separate values Objects: the reference is copied original copy name: 'Amina' age: 21 one object, two names
Figure 1. Changing 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.)

Common mistake

=== 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.
  • typeof returns a type name as a string, but typeof null is "object" and arrays are "object" too.
  • undefined means "not set yet"; null means "deliberately empty".
  • Assigning a primitive copies the value; assigning an object copies a reference to the same object.
Up next · New chapter: JavaScript BasicsOperatorsArithmetic, assignment, comparison (== vs ===) and logical operators.
→

Tip: use the ← and → keys to move between pages.