Analysis JavaScript distinguish three two equal sign and an equal sign (== === and) (rpm)

== equality equivalents, === identity identity.

== different, when value types both sides, first type conversion, and then compared.

===, not cast, different types of a certain range.

The following are instructions:

Let me talk ===, this is relatively simple. The following rules to determine whether two values ​​are equal ===:

1, if a different type, it [unequal]

2, if the two values ​​are, and the same value, then [equal]; (! Exception) is, if at least one is a NaN, then [unequal]. (Determines whether a value is NaN, only with isNaN () to determine)

3, if the two are strings of characters for each position are the same, then the [equal]; otherwise [not equal to].

4, if the two values ​​are true, or are false, then [equal].

5, if the two values ​​refer to the same object or function, then [equal]; otherwise [unequal].

6, if the two values ​​are null, or is undefined, then [equal].

== Besides, according to the following rules:

1, if the same type of two values, for comparing ===.

2, if the two values ​​are different types, they may be equal. The following type conversion rules then compare:

a, if one is null, a is undefined, then [equal].

B, if a string, a numerical value is, the string into a numerical value and then compared.

c, such that either value is true, then convert it to a comparator; if either value is false, then convert it to 0 comparison.

d, if one object and another number or string, the value of the underlying object into another type of comparison. It is converted into an object base type, or using its toString valueOf method. js core built-in classes, will try to precede toString valueOf; exceptions are Date, Date utilization is toString conversion. Js non-core subject, so to say (too much trouble, I do not quite understand)

E, any other combinations are [unequal].

For example:

"1" == true

Type ranging, true value will be converted to 1, now becomes "1" == 1, then "1" into 1, 1 == 1 relatively equal.

Assignment operator =

== equal
=== strict equality

Example:

A =. 3 var;
var = B ". 3";
A == B returns to true
A B returns false ===

Because a, b is not the same type

=== strict comparison to judge

(From Script House)

 

Guess you like

Origin www.cnblogs.com/perfumeBear/p/11776073.html