ChatGPT解决这个技术问题 Extra ChatGPT

Difference between == and === in JavaScript [duplicate]

This question already has answers here: Which equals operator (== vs ===) should be used in JavaScript comparisons? (48 answers) Closed 2 years ago.

What is the difference between == and === in JavaScript? I have also seen != and !== operators. Are there more such operators?

== 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
Ah, yes, the abstract equality operator (==), also known as the if-same-type-then-strict-equality-comparison-otherwise-treat-null-and-undefined-and-document-dot-all-as-equal-but-if-string-involved-with-number-or-bigint-then-coerce-string-to-respective-numeric-type-but-if-boolean-involved-then-coerce-it-to-number-but-if-object-involved-then-coerce-it-to-primitive-and-if-numeric-types-involved-then-compare-their-numeric-values-with-distinct-infinities-and-nans-being-unequal-and-then-repeat-as-needed operator.

u
user229044

Take a look here: http://longgoldenears.blogspot.com/2007/09/triple-equals-in-javascript.html

The 3 equal signs mean "equality without type coercion". Using the triple equals, the values must be equal in type as well.

0 == false   // true
0 === false  // false, because they are of a different type
1 == "1"     // true, automatic type conversion for value only
1 === "1"    // false, because they are of a different type
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false

Thanks for the clear answer! I guess if compared to C# the == would also be == and === would translate to .Equals()
what about "new String()===new String()", both values and types are same. But statement returns false.
@hrishikeshp19: in that case, the values are actually different (different object references)
@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.
@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.
u
user229044

=== and !== are strict comparison operators:

JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and: Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions. Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another. Two Boolean operands are strictly equal if both are true or both are false. Two objects are strictly equal if they refer to the same Object. Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]

Comparison Operators - MDC


So, if I do for example: if (input == null) ..., will it also make the condition true when input is undefined?
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.
-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
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..?
For plain English description of the issue see stackoverflow.com/a/38856418/984780