Basics
Types of values and typeof
Every value is a number, a string, a boolean, a "nothing" or an object — and typeof tells you which.
Why it exists
The same characters mean different things depending on the type: 2 + 3 is 5, but "2" + 3 is "23". A form field always gives you a string, a server may send a number as a string, and a variable you forgot to fill in is undefined. If you don't know what type a variable holds, the program quietly calculates the wrong thing — with no error at all. Types explain why code behaves the way it does, and typeof lets you check a type while the program runs.
Where it's used
- Validating input:
typeof price === "number"before calculating a total. - Debugging:
console.log(value, typeof value)shows at once that a string arrived instead of a number. - Functions that accept different values: a string takes one path, a number another.
- Checking that an optional argument was passed:
typeof callback === "function".
Syntax
typeof value // a string naming the typeExamples
typeof 42; // → "number"
typeof "meow"; // → "string"
typeof true; // → "boolean"
typeof undefined; // → "undefined"
typeof null; // → "object" — a historical bug2 + 3; // → 5
"2" + 3; // → "23"
"Boxes: " + 5; // → "Boxes: 5"
"5" - 2; // → 3 — minus always works with numbers
Number("2") + 3; // → 5+ adds or gluestypeof [1, 2]; // → "object"
Array.isArray([1, 2]); // → true
typeof function () {}; // → "function"
typeof typeof 42; // → "string"Array.isArray is there for itlet value = 10;
typeof value; // → "number"
value = "ten";
typeof value; // → "string"Results after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
JavaScript has primitives — simple values: number (all numbers, whole and fractional), string (text), boolean (true/false), undefined, null, plus the rarer bigint and symbol. Everything else is an object: {}, arrays, functions, dates.
The type belongs to the value, not the variable: the same let variable can hold a number first and a string later. That's why JavaScript is called dynamically typed.
typeof returns a string: "number", "string", "boolean", "undefined", "object", "function", "bigint" or "symbol". Two quirks: for null it returns "object" (a bug from the first version of the language that can't be fixed without breaking old code), and for arrays it also returns "object", because an array is an object.
When an operation gets values of different types, JavaScript converts them to one. For + the rule is "if there's a string, glue", while -, * and / always turn their operands into numbers.
Traps
How it differs from…
When to pick something else
Don't use typeof to check for arrays or null — there are Array.isArray and === null for that. And don't pepper every line with type checks: check data where it comes in from outside (a form, a server), then work with types you already know.
Lessons where this appears
- Byte clears out the atticTale · Chapter 1 · Variables and types
- Why is typeof null "object"?Why? · Chapter 1 · Variables and types
- Labels for the atticWorkshop · Chapter 1 · Variables and types
- The Null GhostBoss · Chapter 1 · Variables and types
- Byte becomes an accountantTale · Chapter 4 · Numbers and Math
- Who reads the diaryTale · Chapter 12 · Scope and closures