标签云

微信群

扫码加入我们

WeChat QR Code

为什么新的字符串(

Why 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.

2018年05月23日06分57秒

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

2018年05月23日06分57秒

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.

2018年05月23日06分57秒

aashnisshah that also returns false

2018年05月23日06分57秒

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

2018年05月23日06分57秒

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

2018年05月24日06分57秒

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

2018年05月24日06分57秒

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.

2018年05月24日06分57秒

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?

2018年05月24日06分57秒

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.

2018年05月23日06分57秒

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

2018年05月24日06分57秒

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

2018年05月23日06分57秒

Updated my answer to clarify a bit more

2018年05月23日06分57秒

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.

2018年05月24日06分57秒

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

2018年05月24日06分57秒

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.

2018年05月23日06分57秒

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.

2018年05月23日06分57秒

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

2018年05月23日06分57秒

"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.

2018年05月24日06分57秒

I like the analogy, +1

2018年05月23日06分57秒