Why JavaScript's [] == ![] is true

JavaScript has a lot of tricky questions, and one of my favorites is this one 👇 console.log([] == ![]) Most developers expect this to be false. But the actual output is: true Why? Step 1: ![] becomes false Because in JavaScript, an empty array is truthy. Step 2: Now the comparison becomes [] == false Step 3: JavaScript converts both values during loose equality comparison false becomes 0 [] becomes "" Then: "" becomes 0 Final comparison: 0 == 0 That’s why the result is true. This is a perfect example of why == can create unexpected results. That’s also why many developers prefer using === for safer and more predictable comparisons. JavaScript is powerful, but type coercion can be surprisingly tricky. Have you seen a stranger JS behavior than this one? 😄 #JavaScript #FrontendDevelopment #WebDevelopment #Programming #SoftwareEngineering #JSInterview #ReactJS

To view or add a comment, sign in

Explore content categories