By santosh kore

2014-09-03 15:00:26 8 Comments

Why does the following statement return false in JavaScript?

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


@BeyelerStudios 2014-09-03 15:03:39

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.

@Alessandro Incarnati 2014-09-03 17:14:02

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.

@BeyelerStudios 2014-09-03 17:19:41

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.

@Cole Johnson 2014-09-03 17:55:36

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

@David Richerby 2014-09-03 22:35:18

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

@Brian Dillingham 2014-09-03 15:06:48

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

@Felix Kling 2014-09-03 15:07:34

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

@santosh kore 2014-09-03 15:21:05

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

@Brian Dillingham 2014-09-03 15:30:12

Updated my answer to clarify a bit more

@Felix Kling 2014-09-03 15:33: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.

@santosh kore 2014-09-03 15:37:15

@FelixKling: Thanks :)

@Brian Dillingham 2014-09-03 15:40:39

@FelixKling, you should combine all of your so awesomely informative comments into an answer that I can upvote.

@Casey Kuball 2014-09-03 19:52:32

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

@Cody 2014-09-03 16:10:45

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

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

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

Heap vs. Stack

@ccorrin 2014-09-03 18:20:28

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.

@Felix Kling 2014-09-03 18:48:38

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

@Brian Dillingham 2014-09-03 20:01:57

I like the analogy, +1

@Pointy 2014-09-03 15:02:09

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.

@Tim Down 2014-09-03 15:04:29

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

@santosh kore 2014-09-03 15:14:37

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

@Felix Kling 2014-09-03 15:23:58

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

@santosh kore 2014-09-03 15:28:54

@FelixKling: Thanks got it :)

@Cole Johnson 2014-09-03 17:23:08

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?

@Pointy 2014-09-03 17:26:32

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

@Inateno 2014-09-03 15:29:08

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.

Related Questions

Sponsored Content

81 Answered Questions

[SOLVED] How can I convert a string to boolean in JavaScript?

  • 2008-11-05 00:13:08
  • Kevin
  • 2045246 View
  • 2584 Score
  • 81 Answer
  • Tags:   javascript

27 Answered Questions

[SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?

22 Answered Questions

[SOLVED] Checking if a key exists in a JavaScript object?

18 Answered Questions

[SOLVED] .prop() vs .attr()

21 Answered Questions

[SOLVED] How do you access the matched groups in a JavaScript regular expression?

  • 2009-01-11 07:21:20
  • nickf
  • 777710 View
  • 1376 Score
  • 21 Answer
  • Tags:   javascript regex

7 Answered Questions

[SOLVED] Why does Google prepend while(1); to their JSON responses?

19 Answered Questions

[SOLVED] Is it possible to apply CSS to half of a character?

  • 2014-05-09 16:16:57
  • Mathew MacLean
  • 243157 View
  • 2828 Score
  • 19 Answer
  • Tags:   javascript html css

9 Answered Questions

[SOLVED] Why does ++[[]][+[]]+[+[]] return the string "10"?

  • 2011-08-26 08:46:14
  • JohnJohnGa
  • 202980 View
  • 1670 Score
  • 9 Answer
  • Tags:   javascript syntax

Sponsored Content