The typeof operator in JavaScript
typeof is an operator that, while the program runs, returns a string describing the type of a value. In TypeScript the typeof keyword used in type position means something else: at compile time it extracts the type of a variable, function or class.
Theory
TL;DR
- In JavaScript
typeofworks at runtime and returns a string, not the type itself. - Possible results:
"number","string","boolean","undefined","object","function","symbol","bigint". typeof nullgives"object", a well known historical bug in the language.- Arrays are technically objects, so
typeof []also gives"object". typeofon an undeclared variable does not throw: it is the safe way to check whether it exists.- In TypeScript
typeofin type position works at compile time and returns a type.
Quick example
console.log(typeof 42); // "number"
console.log(typeof 'hello'); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof { a: 1 }); // "object"
console.log(typeof [1, 2, 3]); // "object" (arrays are objects!)
console.log(typeof null); // "object" (historical language bug)
console.log(typeof function(){}); // "function"The operator returns a string, not the type itself.
What typeof actually reports
| Case | Result | Comment |
|---|---|---|
typeof x (when x is not declared) | "undefined" | Does not throw, so it is safe |
typeof null | "object" | Historical bug in JS |
typeof [] | "object" | Arrays are technically objects |
typeof function() {} | "function" | A special subtype of object |
The main thing to remember: typeof cannot tell an array apart from a plain object or from null. For arrays use Array.isArray(x), and check null with a direct comparison x === null.
Safely checking whether a variable exists
if (typeof user === 'undefined') {
console.log('user is not defined');
}Here typeof lets you check for a variable without an error, even if it was never declared. Any other reference to an undeclared identifier would immediately throw a ReferenceError.
typeof in TypeScript
And here is the key difference.
In TypeScript
typeofis an operator that works with types at compile time, and it returns a type, not a string.
That is, it does not evaluate the value, it takes the type of a variable or expression so that the type can be used in annotations.
const user = {
name: 'Tim',
age: 30
};
// take the type of the user object
type UserType = typeof user;
// equivalent to:
type UserType = {
name: string;
age: number;
};Unlike JavaScript,
typeofhere does not run any code, it simply extracts the type of a variable, function or class.
typeof together with ReturnType
function makeUser() {
return { name: 'Alice', active: true };
}
type User = ReturnType<typeof makeUser>;
// User = { name: string; active: boolean }Here
typeof makeUsergives the type of the function, andReturnType<...>gives the type of the value it returns.
Key differences between JS and TS
| Aspect | typeof in JavaScript | typeof in TypeScript |
|---|---|---|
| When it works | At runtime | At compile time |
| What it returns | A type string ("number", "object", ...) | The type of a variable, function or object |
| Usable in conditions | Yes | No, it only works inside types |
| Example | typeof x === 'string' | type T = typeof x |
If x is not declared | No error | Compilation error |
A contrasting example
// JavaScript
let a = 10;
console.log(typeof a); // "number"// TypeScript
let a = 10;
type AType = typeof a; // AType = numberThe first returns a string at runtime, the second creates a type at compile time.
Common mistakes
- Expecting
"array"fromtypeof []. No such result exists, an array is an object. UseArray.isArray(value). - Relying on
typeof value === 'object'to detect an object.nullpasses that check too. The correct form isvalue !== null && typeof value === 'object'. - Confusing the string with the type. In JS
typeof xis always a string, sotypeof x === Numberis never true, you needtypeof x === 'number'. - Thinking TypeScript's
typeofchecks anything at runtime. In type position it disappears after compilation and does not affect the emitted JavaScript. - Forgetting that
typeofis the only safe way to touch an undeclared variable.if (user === undefined)throws aReferenceError, whileif (typeof user === 'undefined')does not.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.