Suggest an editImprove this articleRefine the answer for “The typeof operator in JavaScript”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**In JavaScript, `typeof` works at runtime and returns a string naming the type of a value: `"number"`, `"string"`, `"boolean"`, `"undefined"`, `"object"`, `"function"`, `"symbol"`, `"bigint"`.** Arrays and `null` both yield `"object"` (for `null` this is a historical bug in the language), while functions get their own `"function"` result. For an undeclared variable `typeof` does not throw a `ReferenceError`, it safely returns `"undefined"`. In TypeScript the same keyword used in type position behaves completely differently: it runs at compile time and produces a type, not a string. ```javascript typeof [1, 2, 3]; // "object" typeof null; // "object" typeof function(){} // "function" ``` **Key point:** in JS `typeof` is a runtime operator that returns a string; in TS it is a compile-time operator that returns a type.Shown above the full answer for quick recall.Answer (EN)Image**`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 | 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 ```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 | 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 // 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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.