Course contentsShow
← TypeScript
Lesson 190 of 1,7965. Type Narrowing FundamentalsFree lesson

The typeof Operator in JavaScript

Review of JavaScript's typeof operator and the seven possible string values it returns.

The typeof Operator in JavaScript

What you'll learn: How JavaScript's typeof operator inspects runtime values and the seven possible string values it can return.

Before we can use type narrowing in TypeScript, we need to understand the JavaScript feature it relies on: the typeof operator.

What typeof Does

The typeof operator is built into JavaScript (and therefore available in TypeScript). It takes a value and returns a string describing what type that value is at runtime.

const x = 42;
console.log(typeof x); // "number"

const y = "hello";
console.log(typeof y); // "string"

The Seven Possible Return Values

The typeof operator can only return seven specific strings:

  1. "string" – for string values
  2. "number" – for numbers (including NaN and Infinity)
  3. "boolean" – for true or false
  4. "undefined" – for undefined
  5. "object" – for objects, arrays, and null (yes, null is quirky!)
  6. "function" – for functions
  7. "symbol" – for symbols
  8. "bigint" – for bigint values
typeof "text";        // "string"
typeof 100;           // "number"
typeof true;          // "boolean"
typeof undefined;     // "undefined"
typeof { a: 1 };      // "object"
typeof [1, 2, 3];     // "object"
typeof null;          // "object" (JavaScript quirk!)
typeof function() {}; // "function"
typeof Symbol();      // "symbol"
typeof 9007199254740991n; // "bigint"

Why This Matters for TypeScript

TypeScript uses typeof checks to narrow types intelligently. When you write if (typeof x === "string"), TypeScript knows inside that block x must be a string.

Key Takeaway: The typeof operator returns one of seven strings describing a value's runtime type, and TypeScript leverages these checks to make code safer through type narrowing.