Data types
Meet JavaScript's seven primitive types and objects, learn what typeof really reports, and see why variables can hold different kinds of values over time.
- 01Separate primitives from objectsPredict when copying makes an independent value and when it shares an object reference.
- 02Name all eight JavaScript typesRecognize strings, numbers, booleans, BigInts, symbols, null, undefined, and objects.
- 03Use typeof honestlyRead real typeof results, including null, arrays, functions, and NaN.
Values have types
Every value in JavaScript has a type: a small label that tells the language what kind of value it is and what operations make sense. The variable name is not the type. The value inside the variable is.
You already met variables in Variables: let, const & var. That lesson separated reassignment (point a variable at a new value) from mutation (change an object through an existing reference). Data types explain why that distinction matters.
JavaScript has eight type categories: seven primitive types plus object. Primitive values are small, immutable values; objects are collections that variables refer to by reference.
If you saw Why JavaScript?, you saw "5" + 3 become "53". That happened because one value was a string and the other was a number. This lesson names those categories without going deep on any one: each type gets its own lesson soon, starting with Numbers & math next.
Primitives vs objects
STEP THROUGHJavaScript values first split into two families: primitives and objects. A primitive is a value itself: 5, "Ada", true, null. You do not open it and change a part of it. If you want a different primitive, you replace it with another primitive.
Objects are different. An object can hold properties, and code can change those properties. Variables do not contain the whole object; they contain a reference to it. Copy the reference, and two variables can reach the same object.
Imagine a as a sticky note with 5 written on it. When b = a, you get another sticky note with 5. Changing b to 6 does not rewrite the first note.
- In real life: A sticky note that says 5
- In JavaScript: A primitive number value
- In real life: Photocopy the note
- In JavaScript: Copying a primitive to another variable
- In real life: Write 6 on the second note
- In JavaScript: Reassign the second variable
- In real life: The first note still says 5
- In JavaScript: The first variable is unchanged
Where the analogy stops: Real sticky notes can be scribbled on. Primitive values cannot be changed in place; the variable is given a replacement value.
An object is more like a house. The variable holds the address. Copying the address does not build a second house. If someone changes the house, everyone with that address sees the change.
- In real life: A house
- In JavaScript: An object like
{ n: 1 } - In real life: An address card
- In JavaScript: A variable that refers to the object
- In real life: Copy the address card
- In JavaScript: Assign one object variable to another
- In real life: Paint the house door
- In JavaScript: Mutate a property; every copied address sees it
Where the analogy stops: JavaScript references are not literal street addresses and you cannot do address arithmetic. The useful idea is sharing: two references can reach one object.
Predict both printed values. Then step through the primitive copy and the object-reference copy.
script
let b = a;b = 6;console.log(a); const box1 = { n: 1 };const box2 = box1;box2.n = 2;console.log(box1.n);The first half prints 5 because numbers are primitives copied by value. The second half prints 2 because both names point at one object. Later lessons, Objects & references and Memory: primitives vs objects, go deeper into references. For now, hold onto the prediction rule: primitives copy the value; object variables copy the reference.
The eight types
TABLEHere is the whole pantry. Seven types are primitive: string, number, bigint, boolean, undefined, symbol, and null. The eighth type is object, which covers plain objects, arrays, dates, functions, Math, and most structured things you work with.
Think of the eight types as pantry shelves. You do not memorize every can on day one. You learn the shelf names so you know where to look next.
- In real life: A shelf for flour, a shelf for sugar
- In JavaScript: A type category for values
- In real life: A recipe asks for an ingredient kind
- In JavaScript: An operation expects a kind of value
- In real life: One pantry contains many brands
- In JavaScript: One type contains many possible values
Where the analogy stops: Real ingredients can sometimes be swapped by taste. JavaScript has precise rules; if a value is the wrong type, it may convert it or produce a surprising result.
| Type | Example literal | typeof result | Notes |
|---|---|---|---|
| string | "hi" | string | Text. Covered next in the Strings lesson. |
| number | 42 | number | Integers, decimals, NaN, and Infinity. Next: Numbers & math. |
| bigint | 10n | bigint | Whole numbers beyond safe number range. See the BigInt lesson. |
| boolean | true | boolean | True or false values. See Booleans, null & undefined. |
| undefined | undefined | undefined | Usually means no value has been assigned yet. |
| symbol | Symbol("id") | symbol | Unique primitive identifiers. |
| null | null | object | An intentional empty value; typeof says object because of history. |
| object | {} | object | Objects, arrays, functions, dates, Math, and more. |
A few notes keep this table honest. null is listed with primitives because it is a primitive value, even though typeof null reports "object". Functions are objects, even though typeof reports "function". Arrays are objects too; JavaScript gives you Array.isArray for that specific check.
42"text"truenullundefined10nSymbol()[1, 2]{ a: 1 }function () {}new Date()Math
Sort each value into the primitive family or the object family. Read the explanation for every edge case.
The typeof operator
INTERACTIVEtypeof is an operator: a built-in piece of JavaScript syntax that asks “what type name should I report for this value?” It returns a string such as "number" or "object".
const value = null;console.log(typeof value);console.log(Array.isArray(value));null gives typeof "object". typeof null is object, a historical bug kept so old websites do not break.
typeof operator in this browser. The source shows the shape; the selected value changes line 1.Use typeof for broad checks: string, number, bigint, boolean, undefined, symbol, function, or object. Then patch the known rough edges:
typeof nullis"object", a historical bug kept for compatibility. Checkvalue === nullfirst when null matters.typeof []is"object". UseArray.isArray(value).typeof function ()is"function". Functions are callable objects; this special result is useful.typeof NaNis"number".NaNis a special number value.
function describe(value) {
if (typeof value === "array") {
return "array";
}
return typeof value;
}
console.log(describe([1, 2]));There is no "array" result from typeof, so this program prints "object". The fix is a specific array check:
function describe(value) {
if (Array.isArray(value)) {
return "array";
}
return typeof value;
}
console.log(describe([1, 2]));Dynamic typing
STEP THROUGHJavaScript is dynamically typed. That means variables do not have fixed type declarations. A variable made with let can hold a number now, a string later, and a boolean after that. The type travels with the current value.
Picture a reusable box named value. The box name stays the same, but the label belongs to what is inside it. Put in 42 and the label says number. Swap in "forty-two" and the label says string.
- In real life: A box named value
- In JavaScript: The variable name
- In real life: A label on the item inside
- In JavaScript: The value's type
- In real life: Swap a toy for a book
- In JavaScript: Reassign a different type of value
Where the analogy stops: A physical box can hold many things at once. A JavaScript variable holds one value at a time; reassignment replaces that value.
Watch one variable hold a number, then a string, then a boolean. Each typeof result follows the current value.
script
console.log(typeof value);value = "forty-two";console.log(typeof value);value = true;console.log(typeof value);Dynamic typing is convenient for learning and quick scripts, but it also means you must be clear about what kind of value your code expects. TypeScript, a later lesson, adds static type checking on top of JavaScript so tools can warn you before the program runs.
Where you’ll use this
Type checks appear when code receives a value from outside: a form input, a configuration object, a saved setting, or an API response. Early lessons keep examples tiny, but professional code uses the same moves.
function describeValue(value) {
if (Array.isArray(value)) return "array";
if (value === null) return "null";
return typeof value;
}
console.log(describeValue(["Ada", "Lin"]));
console.log(describeValue(null));This function handles the two typeof traps first: arrays and null. Only then does it return the broad typeof result. The deeper lesson Checking types reliably will build this into a stronger checklist.
Common misconceptions
“typeof tells me every exact kind.”
It reports broad type names. Arrays, dates, and plain objects all report "object"; use more specific checks when needed.
“null is an object.”
null is a primitive value. Only typeof null says "object", and that is historical compatibility.
“const makes objects immutable.”
const prevents reassignment of the variable. It does not freeze the object; const box = { n: 1 } can still have box.n changed.
“A copied object is a new object.”
Assignment copies the reference. Two variables can point at one object until you create a real copy.
“NaN must have its own type.”
NaN is a number value. It means a numeric calculation failed, not that JavaScript made a ninth type.
Practice: spot the type
5 EXERCISESPredict the four printed lines.
console.log(typeof 42);
console.log(typeof null);
console.log(typeof [1, 2]);
console.log(typeof NaN);The outputs are number, object, object, and number. null and arrays are the object-looking quirks; NaN is still a number value.
The function tries to detect arrays with typeof. What expression should replace the broken condition?
function describe(value) {
if (typeof value === "array") {
return "array";
}
return typeof value;
}
console.log(describe([1, 2]));function describe(value) {
if (Array.isArray(value)) {
return "array";
}
return typeof value;
}
console.log(describe([1, 2]));Use Array.isArray(value). It returns true for arrays and false for other objects, so the program prints array.
Is Math a primitive or an object?
Math is an object. It groups constants and functions such as Math.PI and Math.round.
Predict the output, then explain in your own words why it happens.
const box1 = { n: 1 };
const box2 = box1;
box2.n = 2;
console.log(box1.n);It prints 2. box1 and box2 refer to the same object, so changing box2.n changes the object that box1.n reads too.
How many primitive types does JavaScript have?
console.log(["string", "number", "bigint", "boolean", "undefined", "symbol", "null"].length);There are seven primitive types: string, number, bigint, boolean, undefined, symbol, and null. Object is the eighth type category.
Quiz: check your understanding
7 QUESTIONSEvery answer explains itself. For code questions, predict first, then choose.
Question 1 of 7Which statement best describes a primitive value?
Choose an answer to see the explanation.
Question 2 of 7After copying a primitive, what does this print?
Read the code, then predictlet a = 5; let b = a; b = 6; console.log(a);Choose an answer to see the explanation.
Question 3 of 7Which list contains only primitive types?
Choose an answer to see the explanation.
Question 4 of 7What does typeof null print?
Read the code, then predictconsole.log(typeof null);Choose an answer to see the explanation.
Question 5 of 7How should you check whether a value is an array?
Choose an answer to see the explanation.
Question 6 of 7After reassignment to text, what does this print?
Read the code, then predictlet value = 42; value = "forty-two"; console.log(typeof value);Choose an answer to see the explanation.
Question 7 of 7What does typeof NaN print?
Read the code, then predictconsole.log(typeof NaN);Choose an answer to see the explanation.
Key takeaways
- JavaScript has eight type categories: seven primitives plus object.
- Primitives are immutable values; objects are mutable collections reached through references.
typeofis useful, but remembernull, arrays, functions, andNaN.- Variables are dynamically typed: the current value has the type, not the variable name.
- Use
Array.isArrayfor arrays andvalue === nullfor null.
One-liner.
A JavaScript data type tells you what kind of value you have, and that determines how copying, checking, and operations behave.
Up next: Numbers & math.