标签云

微信群

扫码加入我们

WeChat QR Code

This question already has an answer here:Which equals operator (== vs ===) should be used in JavaScript comparisons?49 answersJavaScript comparison operators: Identity vs. Equality4 answersWhy does the following statement return false in JavaScript?new String('hello') === new String('hello')


It's probably comparing the location of the strings, rather than the actual strings. If you save the strings to variables, then compare the variables, it should return true.

2019年05月22日41分48秒

For the same reason [] === [] is false.

2019年05月22日41分48秒

For reference: stackoverflow.com/a/3586788/899126. Basically, it's because the comparison isn't being done between two strings, but two objects. You can call .valueOf() on the string objects, and your comparison will return true.

2019年05月22日41分48秒

aashnisshah that also returns false

2019年05月22日41分48秒

aashnisshah: Variables don't have any impact on comparing values. They are just placeholders.

2019年05月22日41分48秒

If the operands are of the same type then == and === are identical, so new String('hello') == new String('hello') is also false.

2019年05月22日41分48秒

thanks, but still i am not sure why if a = {} and b = {}, then a === b and a == b returns false

2019年05月22日41分48秒

santoshkore: Because you are comparing two different objects. A car manufacturer produces two cars of the same model. The cars are similar, but they are not the same. When you compare objects, you are testing for identity, not similarity.

2019年05月22日41分48秒

What if string interning were to be used? Would the two different objects be interned into one? Or is string interning only used on string primitives?

2019年05月22日41分48秒

ColeJohnson I don't think the JavaScript interpreter is allowed to coalesce two distinct objects; it would be weird behavior. Note that the string primitive inside a String instance can itself be interned. There's not much to a String instance, really, other than a reference to the primitive and the link to its prototype.

2019年05月22日41分48秒

What's the difference here? You should elaborate on that (since you are not directly answering the OP's question).

2019年05月22日41分48秒

new String("hello") === new String("hello") check type as well as reference that is why it returns false got it but new String("hello") == new String("hello") is comparing the objects instead of the value what this means? I was always thinking == compares only values. Please elaborate

2019年05月22日41分48秒

Updated my answer to clarify a bit more

2019年05月22日41分48秒

santoshkore: Objects are values. They are values of the Object data type. The difference is that String() returns a primitive value and new String() returns an object value. Primitive values are not unique like objects are, they don't have an ID. So when you compare them, there is nothing else to compare as their raw data. Each object is unique though, so if you compare objects you are comparing their identities.

2019年05月22日41分48秒

To others: notice the lack of a new keyword in the String comparison in this answer.

2019年05月22日41分48秒

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference.Link-only answers can become invalid if the linked page changes.

2019年05月22日41分48秒

while this is true, I suspect stackoverflow handling it's own links once they were to change their url-generation. the question was answered quickly by others so I didn't elaborate further.

2019年05月22日41分48秒

You still should not have answers that are just a link.

2019年05月22日41分48秒

"You are asking javascript to compare two different instances of the variable" Is very confusing. Variables are simply containers for values, you can't create an "instance" of a variable.

2019年05月22日41分48秒

I like the analogy, +1

2019年05月22日41分48秒