typeof vs instanceof

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: typeof vs instanceof

Approach

Compare typeof (returns a string for primitive types and "object"/"function" for references) with instanceof (walks the prototype chain). Highlight the classic gotchas: typeof null returns "object", typeof [] returns "object", and show Array.isArray as the reliable array check.

Complexity Analysis

Time
O(1)
Space
O(1)

Pattern

Type Checking Patterns

Why It Works

typeof operates on the internal type tag of a value (with the null bug being a legacy spec issue), while instanceof relies on the prototype chain. Neither alone covers all cases, so combining them or using Array.isArray is necessary.

Updated Mar 2026