Our code :-
🔹 Line 1: [] == 0
Step-by-step:
- Operands:
- Left:
[]
→ an empty array (object). - Right:
0
→ a number. - Loose equality
==
allows type coercion, so JavaScript tries to make the types match. []
is an object. When comparing an object to a primitive, JavaScript first converts the object to a primitive usingToPrimitive
.- So:
[]
→""
(an empty string)-
(because
.toString()
on an empty array returns""
) Now the expression becomes:
- "" == 0
Next, JavaScript converts both sides to numbers:
""
→0
0
stays0
Now it’s:
- 0 == 0 // true
✅ Result: true
🔹 Line 2: [0] == [0]
Step-by-step:
- Both sides are arrays, each containing a single element:
0
. - Arrays are objects in JavaScript, and objects are compared by reference, not by value.
[0] == [0]
is comparing:- Two different array instances (in memory), even though they contain the same value.
Since they’re not the same object in memory:
- false
✅ Result: false
✔ Final Answer:
A) true false
This question appeared in the Tech Mahindra 2nd round of interview, testing the candidate’s knowledge of comparisons operator between arrays in JavaScript.