Why does new String('hello') === new String('hello') evaluate to False? [duplicate]

£可爱£侵袭症+ 提交于 2019-11-30 04:39:09
Pointy

Two String objects will always be unequal to each other. Note that JavaScript has string primitive values as well as a String constructor to create wrapper objects. All object equality comparisons (especially with ===) are carried out as a test for reference equality. References to two different objects will of course never be equal to each other.

So "hello" === "hello" will be true because those are string primitives.

You are comparing object instances, which is not like a string comparison ('hello' === 'hello') Comparing objects in Javascript is actually comparing the memory addresses of the objects and will always return false because memory addresses are different for each object.

Compare the string values instead of the object instance - jsFiddle

( String('hello') === String('hello') ) // returns true due to comparing strings

Strictly comparing two objects - false not the same object

new String('hello') === new String('hello')

Strictly comparing two strings - true, same returned value and same returned type

String('hello') === String('hello')
BeyelerStudios

It evaluates to false because you're comparing two different objects: new will create a new object.

Related post: What is the 'new' keyword in JavaScript? Which explains in its (extensive) answer:

It [new] is 4 things:

  1. It creates a new object. The type of this object, is simply object.
  2. It sets this new object's internal, inaccessible, [[prototype]] property to be the constructor function's external, accessible, prototype object (every function object automatically has a prototype property).
  3. It executes the constructor function, using the newly created object whenever this is mentioned.
  4. It returns the newly created object, unless the constructor function returns a non-primitive value. In this case, that non-primitive value will be returned.

You are asking javascript to compare two different instances of the variable, not the string value that lives inside the variable.

So for example, lets say I have a piece of paper with the word "Hello World" written on it (Paper1) and my brother has a different piece of paper with the word "Hello World" written on it (Paper2).

When you say is Paper1 === Paper2 you will get false, beacuse no they are not the exact same piece of paper, even though the words written on the paper are the same.

If you where to say Paper1.toString() === Paper2 .toString() you would get true, beacuse we are comparing the words written on the paper, not the actual paper itself.

Cody
typeof(new String()) === 'object';
==> true

(new Object()) === (new Object());
==> false

Any "object" structure in the "Heap" is unique;

Heap vs. Stack

Also if you do if ( { hello: 1 } === { hello: 1 } ){ console.log( "yay" ); } the console.log never happen, because it's an object.

You can compare 2 literal objects (as my first example) by making a loop on these objects and when you find a difference you know the result. It's more difficult to do this trick in an instantiated object, compare 2 functions it's crazy.

But if JavaScript don't do it for you it's because this is very heavy, you have check each type of each attributes to stringify it if it's a function etc... and obviously it's not useful to do it.

You can use instanceof if you want to check 2 objects "origins", because typeof will return you "object". And for testing 2 "new String" object you have to use toString new String( "hello" ).toString() == new String( "hello" ).toString() or if you want to check the object without testing the attributes new String( "hello" ) instanceof String && new String( "hello" ) instanceof String

is true.

The link given by BeyelerStudios explain perfectly what the new do, hope it'll help.

Your code essentially says "Take a piece of paper and write 'hello' on it. Take another piece of paper and write 'hello' on that. Are they the same piece of paper?"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!