typeof vs instanceof Playground

Bayu’s sorting cabinet: typeof picks one of 8 drawers, instanceof walks the prototype chain. Arrays from another iframe included. An interactive typeof vs…

Why it matters

A wrong type check is a bug at run time. typeof [] === "object" makes people take an array for a plain object, typeof null === "object" lets null through, and an array from another iframe fails instanceof Array. Knowing when to use typeof, instanceof or a special tool is truly defensive code.

The key idea

typeof for primitives, instanceof for objects

Common mistakes

Try it in this playground