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

JavascriptEqualityComparison OperatorsEquality OperatorIdentity Operator

Javascript Problem Overview


Why does the following statement return false in JavaScript?

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

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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')

Solution 3 - Javascript

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

Related post: https://stackoverflow.com/questions/1646698/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.

Solution 4 - Javascript

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.

Solution 5 - Javascript

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

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

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

Heap vs. Stack

Solution 6 - Javascript

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

Solution 7 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsantosh koreView Question on Stackoverflow
Solution 1 - JavascriptPointyView Answer on Stackoverflow
Solution 2 - Javascriptim_brian_dView Answer on Stackoverflow
Solution 3 - JavascriptBeyelerStudiosView Answer on Stackoverflow
Solution 4 - JavascriptccorrinView Answer on Stackoverflow
Solution 5 - JavascriptCodyView Answer on Stackoverflow
Solution 6 - JavascriptDavid RicherbyView Answer on Stackoverflow
Solution 7 - JavascriptInatenoView Answer on Stackoverflow