JavaScript - typeof Operator

Overview

Estimated time: 10–20 minutes

typeof inspects types at runtime. Learn its return values and historical quirks.

Learning Objectives

  • Use typeof for primitives and functions.
  • Handle edge cases like null and arrays.

Prerequisites

Examples

typeof 'hi'        // 'string'
typeof 42           // 'number'
typeof 10n          // 'bigint'
typeof true         // 'boolean'
typeof Symbol('x')  // 'symbol'
typeof undefined    // 'undefined'
typeof null         // 'object' // quirk
typeof {}           // 'object'
Array.isArray([])   // true
typeof (() => {})   // 'function'

Common Pitfalls

  • typeof null is 'object'; check value === null explicitly.