I put the following into the browser console:
0 === {} // false
{} === 0 // SyntaxError: expected expression, got '==='
Why is this?
javascript compares objects by identity, not value. Each object, each {} is distinct.
Because [] creates a new array, so you are comparing one array object with another array object. It's not the contents of the arrays that is compared, the object references are compared. They are not equal because it's not the same object instance.
Note that === never causes type coercion, but checks for correct types first and yields false if they are not equal!
In strict mode, it is now undefined . When a function was called with call or apply , if the value was a primitive value, this one was boxed into an object (or the global object for undefined and null ). In strict mode, the value is passed directly without conversion or replacement.
{} === 0
here {}
is block statement not object literal.
But if you do say var a = {} === 0
it would work
var a = {} === 0
console.log(a);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With