标签云

微信群

扫码加入我们

WeChat QR Code


Here is detailed comparison of both operator in JavaScript with performance test

2019年02月24日11分01秒

== is === with type converting (aka coercion). To really understand what I mean you can look at this JavaScript function that behaves exactly like ==: stackoverflow.com/a/38856418/984780

2019年02月24日11分01秒

In addition to === and !== in JS they should also add +== and -== to increment or decrement only if same type.

2019年02月24日11分01秒

So, if I do for example: if (input == null) ..., will it also make the condition true when input is undefined?

2019年02月24日11分01秒

The above makes it sound as though a == comparison wouldn't check all the things in the first bullet point, "the same sequence of characters, same length, and same characters in corresponding positions" but in fact it does. As far as I can tell the only real difference when comparing two strings is that with ===, new String()===new String() returns false (different object references). But new String should be avoided anyway.

2019年02月24日11分01秒

-1 The question was "what is the difference?" and you only explained the strict operators, but not the difference between them and the non-strict ones

2019年02月24日11分01秒

I didn't exactly get "Two objects are strictly equal if they refer to the same Object" - what? by two objects, does it mean two reference variables..?

2019年02月24日11分01秒

For plain English description of the issue see stackoverflow.com/a/38856418/984780

2019年02月24日11分01秒

Thanks for the clear answer! I guess if compared to C# the == would also be == and === would translate to .Equals()

2019年02月24日11分01秒

what about "new String()===new String()", both values and types are same. But statement returns false.

2019年02月24日11分01秒

hrishikeshp19: in that case, the values are actually different (different object references)

2019年02月24日11分01秒

KoenZomers I don't think your C# case is right. Actually there are no equivalents in C#. == in C# do a reference compare, and Equals do predefined compare, none of them have equivalents in JavaScript either.

2019年02月24日11分01秒

hrishikeshp19, new String() is not of a string type, it's of an object type, so the === rule for objects applies. Usage of primitive strings, however, often results in coercing the strings into String objects, so the difference is subtle. If you were to assign new String() to two different objects, s1 and s2, the valueOf() method on each would return a string primitive for each, and s1.valueOf() === s2.valueOf() would return true.

2019年02月24日11分01秒