Home  >  Article  >  Web Front-end  >  What is the difference between two and three = equal signs in JavaScript_Basic knowledge

What is the difference between two and three = equal signs in JavaScript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:19:061292browse

One equal sign is the function of assignment. The main problem lies in the difference between two and three equal signs.
The difference between two equal signs and three equal signs in javaScript

In a nutshell: == convert the type first and then compare, === determine the type first, if it is not the same type directly is false.
=== means equality, the two sides of the comparison must be absolutely the same

alert(0 == ""); // true
alert(0 == false); // true
alert("" == false); // true

alert(0 === ""); // false
alert(0 === false); // false
alert("" === false); // false

Let’s talk about === first. This is relatively simple. The specific comparison rules are as follows:
1. If the types are different, [not equal]
2. If both are numerical values ​​and are the same value, then [equal]; (!Exception) is that if at least one of them is NaN, then [not equal]. (To determine whether a value is NaN, you can only use isNaN() to determine)
3. If both are strings and the characters at each position are the same, then [equal]; otherwise [not equal].
4. If both values ​​are true, or both are false, then [equal].
5. If both values ​​refer to the same object or function, then [equal]; otherwise [not equal].
6. If both values ​​are null, or both are undefined, then [equal].

Let’s talk about ==. The specific comparison rules are as follows:

1. If the two value types are the same, perform === comparison. The comparison rules are the same as above.
2. If the two value types are different , they may be equal. Perform type conversion and then compare according to the following rules:
a. If one is null and the other is undefined, then [equal].
b. If one is a string and the other is a numerical value, convert the string into a numerical value and then compare.
c. If any value is true, convert it to 1 and compare; if any value is false, convert it to 0 and compare.
d. If one is an object and the other is a numeric value or string, convert the object into a value of the basic type and then compare. The object is converted to the base type using its toString or valueOf method. JS core built-in classes will try valueOf before toString; the exception is Date, which uses toString conversion. Non-js core objects, such as (it’s more troublesome and I don’t quite understand)
e, any other combination (array array, etc.), are [not equal].

When a person can’t find a way out, the best way is to do what he can do well at the moment to the extreme, to the point that no one can do it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn