4

I am trying to compare using the following below formats :

var u=new String('test');
var t=new String('test');

When i check (u==t) or (u===t) it returns false.

Similarly if I try to do

var u=new String();
var t=new String();
u='test';t='test';

Now (u==t) and (u===t) returns true.

4
  • 4
    First two created using String constructor are objects and objects cannot be compared in JS. The last two are created as object and then overwritten by String, so it works. Commented Dec 22, 2015 at 5:23
  • As far as running u='test';t='test'; after defining them using new String(), you are now setting both variables to primitive string with value of 'test'. Commented Dec 22, 2015 at 5:25
  • In the first case you are comparing different string objects. In the second case you are comparing string literals. Commented Dec 22, 2015 at 5:26
  • Objects, including new String instances, are compared by their identity and that's different for every new instance created. So, 2 distinct object can never be equal. Primitives, as you're using the 2nd example (the objects are replaced, not modified), are compared by their values. Commented Dec 22, 2015 at 5:29

5 Answers 5

3

When 2 objects(For eg, objects created using new String()) are compared using ==, behind the scenes === is used. Which makes

u == t 

equivalent to

u === t

And since they are 2 different objects, the result is always false.

However, when you use a string literal, you are creating primitive data, and their comparison is done by the value and not the reference. Which is why u==t returns true, as mentioned in the comments.

Sign up to request clarification or add additional context in comments.

Comments

2

Where the first comparison is comparing object references using the String constructor, the second example you are reinitializing the u and t values to String literals. The first one is actually comparing references where the second is actually comparing values. If you want to comapre the first example by value, you would compare the value of the String object like so.

u.valueOf() === t.valueOf();

Comments

1

You are creating new objects with new String() and referred to MDN's Comparison operators :

If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

Comments

1

When using the new String constructor you're creating objects rather than strings. So when you compare them, they are not equal.

var t = new String("testing");
typeof t;
// "object"

var u = "testing";
typeof u;
// "string"

t == u
// false

However, if you compared their string values:

t.toString() == u.toString()
// true

they would be.

Comments

1

Basically in first case you creating object. so comparing to object do a === so its always return false.

in second case you doing the same thing in first two line with empty string.

var u=new String();
var t=new String();

use console.dir(u) to see.
but in bottom line u='test';t='test'; you overwrite u,t values by string primitive values. so its just comparing value of test

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.