JavaScript - Data Types

Overview

Estimated time: 20–30 minutes

JavaScript has seven primitive types and objects. typeof helps inspect types—with a few quirks.

Learning Objectives

  • List JS primitive types and objects.
  • Use typeof and understand edge cases.

Prerequisites

Primitives

  • string
  • number
  • bigint
  • boolean
  • undefined
  • symbol
  • null

typeof examples

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

Objects

Arrays, functions, dates, maps, sets, and user-defined objects are all objects. Use Array.isArray for arrays.

Common Pitfalls

  • typeof null returns 'object' (quirk). Check value === null explicitly.