Javascript's string comparison - Stack Overflow

admin2025-04-17  1

I am trying to pare 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.

I am trying to pare 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.

Share edited Dec 22, 2015 at 5:33 DhruvJoshi 17.2k6 gold badges45 silver badges62 bronze badges asked Dec 22, 2015 at 5:22 subi_speedrunnersubi_speedrunner 9213 gold badges9 silver badges18 bronze badges 4
  • 4 First two created using String constructor are objects and objects cannot be pared in JS. The last two are created as object and then overwritten by String, so it works. – Tushar 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'. – Will Commented Dec 22, 2015 at 5:25
  • In the first case you are paring different string objects. In the second case you are paring string literals. – user663031 Commented Dec 22, 2015 at 5:26
  • Objects, including new String instances, are pared 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 pared by their values. – Jonathan Lonowski Commented Dec 22, 2015 at 5:29
Add a ment  | 

5 Answers 5

Reset to default 3

When 2 objects(For eg, objects created using new String()) are pared 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 parison is done by the value and not the reference. Which is why u==t returns true, as mentioned in the ments.

Where the first parison is paring 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 paring references where the second is actually paring values. If you want to apre the first example by value, you would pare the value of the String object like so.

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

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

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

When using the new String constructor you're creating objects rather than strings. So when you pare 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 pared their string values:

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

they would be.

Basically in first case you creating object. so paring 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 paring value of test

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744856924a270883.html

最新回复(0)