JavaScript traps · JavaScript

JavaScript: typeof null = "object"?!PRO

Watch the video

A 1995 bug that can never be fixed. Check null directly, and arrays with Array.isArray().

// typeof null = "object" ?! — a bug from the first JavaScript (1995) that can never be fixed.
console.log(typeof null);          // "object" ?!
console.log(typeof undefined);     // "undefined"
console.log(typeof []);            // "object" too ?!

// Fix: check null directly, and arrays with Array.isArray
const x = null;
console.log(x === null);           // true ✓
console.log(Array.isArray([]));    // true ✓
Open code JS4 in Pocket Compiler Download the Pocket Compiler app — free

No app yet? Install it, then open the app and type the code JS4 in Code Library.