Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
5  
1  
 
"Everything in JS is an object" in the sense that even string primitives "inherits" from String prototype. But JS does have "primitivey" values which are not compared as objects. You can check this basic type with the typeof operator. And the spec for ===: es5.github.io/#x11.9.6 –  Fabrício Matté May 21 at 23:44
 
@FabrícioMatté that is not true. String primitives absolutely do not "inherit" from the String prototype. They are a separate type, not objects. Same goes for boolean values and numbers. –  Pointy May 21 at 23:45
 
@Pointy what I meant is ''.toString === String.prototype.toString, could you phrase it better? –  Fabrício Matté May 21 at 23:47
show 6 more comments

marked as duplicate by Fabrício Matté, Matt Ball, Rachel Gallen, hexblot, thepoosh May 22 at 6:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 4 down vote accepted

JavaScript basically treats strings and numbers as scalars at all times, converting them to objects when a method is called, and converting back afterward, in cases where you aren't explicitly declaring new String("");

Same with numbers.

Without string/number/boolean equality, you'd have a hard time doing much of anything.

share|improve this answer
 
"and converting back afterward" nothing is converted back. Wrapper object is just thrown away. –  zerkms May 21 at 23:47
 
@zerkms Yes. You're correct. But in terms of the behind-the-curtain magic, nobody sees the wrapper in the first place. The string is a primitive until it needs to be an object, and then it's an object for the extent of its method calls, and then it's a primitive again. The actual implementation of the feature is quite different than the perceived behaviour (much like ++i vs i++). –  Norguard May 21 at 23:50
 
anyway, I wouldn't say something is converted back, just because it's not correct ;-) –  zerkms May 21 at 23:51
 
@Norguard Yes, without it it'd be hard, but I was curious about the behind the scenes magic. Thank you! –  Oscar Godson May 21 at 23:52
 
@zerkms Just to be sure, instead of "converting", would you say that the primitive/scalar value (which is generated by the implicit String object when modified by a method) is returned then? –  Fabrício Matté May 22 at 0:05
show 8 more comments

It is a one-off.

Reference

There is a difference between string literals and string objects. The article goes into more detail if you are interested.

The same is true for booleans and numbers. These primitives are compared different from objects.

share|improve this answer
add comment

There are five primitive types in JavaScript: Number, String, Boolean, Undefined, and Null. Comparing the empty string literal "" to itself is no different from the comparison 5 === 5.

share|improve this answer
2  
No, it doesn't: f.cl.ly/items/3V3c0G3y0k1x2p1X0Q1Q/… –  Oscar Godson May 21 at 23:54
 
Good catch. It seems I answered too quickly, before understanding the difference between a string literal and a string object. The answer has been edited to be correct. –  Alec Martin May 22 at 0:12
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.