JavaScript traps · JavaScript

JavaScript: [] == ![] is TRUE?!PRO

Watch the video

An empty array equals NOT itself: == turns both sides into the number 0. Use === instead.

// [] == ![] is TRUE ?!
// ![] is false (an array is truthy). Then == converts both sides to numbers:
// [] → "" → 0 and false → 0, so 0 == 0.
console.log([] == ![]);    // true ?!

console.log(![]);          // false
console.log(Number([]));   // 0
console.log(Number(false)); // 0
console.log([] === ![]);   // false ✓ === never converts types
Open code JS8 in Pocket Compiler Download the Pocket Compiler app — free

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