Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Why does the following statement return false in JavaScript?

new String('hello') === new String('hello')
like image 446
santosh kore Avatar asked Sep 03 '14 15:09

santosh kore


People also ask

Why === is false in JavaScript?

Because == (and === ) test to see if two objects are the same object and not if they are identical objects.

What happens when we use new String?

new String("text"); explicitly creates a new and referentially distinct instance of a String object; String s = "text"; may reuse an instance from the string constant pool if one is available.

What is difference between String and new String?

String strLiteral = "Java"; Both expressions give you a String object, but there is a subtle difference between them. When you create a String object using the new() operator, it always creates a new object in heap memory.

Which is faster == or === in JavaScript?

So === faster than == in Javascript === compares if the values and the types are the same. == compares if the values are the same, but it also does type conversions in the comparison. Those type conversions make == slower than ===.


3 Answers

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.

like image 63
Pointy Avatar answered Oct 10 '22 03:10

Pointy


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')
like image 39
im_brian_d Avatar answered Oct 10 '22 01:10

im_brian_d


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.
like image 33
BeyelerStudios Avatar answered Oct 10 '22 01:10

BeyelerStudios