Skip to main content

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 typeof works at runtime and returns a string, not the type itself.
  • Possible results: "number", "string", "boolean", "undefined", "object", "function", "symbol", "bigint".
  • typeof null gives "object", a well known historical bug in the language.
  • Arrays are technically objects, so typeof [] also gives "object".
  • typeof on an undeclared variable does not throw: it is the safe way to check whether it exists.
  • In TypeScript typeof in type position works at compile time and returns a type.

Quick example

javascript
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

CaseResultComment
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

javascript
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 typeof is 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.

typescript
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, typeof here does not run any code, it simply extracts the type of a variable, function or class.

typeof together with ReturnType

typescript
function makeUser() { return { name: 'Alice', active: true }; } type User = ReturnType<typeof makeUser>; // User = { name: string; active: boolean }

Here typeof makeUser gives the type of the function, and ReturnType<...> gives the type of the value it returns.

Key differences between JS and TS

Aspecttypeof in JavaScripttypeof in TypeScript
When it worksAt runtimeAt compile time
What it returnsA type string ("number", "object", ...)The type of a variable, function or object
Usable in conditionsYesNo, it only works inside types
Exampletypeof x === 'string'type T = typeof x
If x is not declaredNo errorCompilation error

A contrasting example

javascript
// JavaScript let a = 10; console.log(typeof a); // "number"
typescript
// TypeScript let a = 10; type AType = typeof a; // AType = number

The first returns a string at runtime, the second creates a type at compile time.

Common mistakes

  • Expecting "array" from typeof []. No such result exists, an array is an object. Use Array.isArray(value).
  • Relying on typeof value === 'object' to detect an object. null passes that check too. The correct form is value !== null && typeof value === 'object'.
  • Confusing the string with the type. In JS typeof x is always a string, so typeof x === Number is never true, you need typeof x === 'number'.
  • Thinking TypeScript's typeof checks anything at runtime. In type position it disappears after compilation and does not affect the emitted JavaScript.
  • Forgetting that typeof is the only safe way to touch an undeclared variable. if (user === undefined) throws a ReferenceError, while if (typeof user === 'undefined') does not.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.