This question already has an answer here:
Everything in JS is an object. I've always known that, and I totally understand that. I know why {} !== {}
. It's two different objects. Same as if you were to write out new Object() == new Object()
.
Some other examples:
{} == {} // => false
[] == [] // => false
/ / == / / // => false
new String() == new String() // => false
But, Strings are objects too (it's why you can do ''.replace()
and extend them), so why does this work:
'' == '' // => true
Obviously it'd be a huge headache to compare two strings if this didn't work, but this seems inconsistent with the rest of the language. Internally, what's going on? Is it just a one-off or is there some other concept behind this?
typeof
operator. And the spec for===
: es5.github.io/#x11.9.6 – Fabrício Matté May 21 at 23:44''.toString === String.prototype.toString
, could you phrase it better? – Fabrício Matté May 21 at 23:47