Home > Web Front-end > JS Tutorial > Introduction to the difference between two equal signs and three equal signs in javaScript_Basic knowledge

Introduction to the difference between two equal signs and three equal signs in javaScript_Basic knowledge

WBOY
Release: 2016-05-16 16:43:02
Original
1459 people have browsed it

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

alert(0 == ""); // true
alert(0 == false); // true
alert("" == false); // true
Copy after login
alert(0 === ""); // false
alert(0 === false); // false
alert("" === false); // false
Copy after login

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 numeric 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].

As for ==, 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 two value types are different, they may be equal. Perform type conversion and comparison 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 numerical 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, Ling Shuo (it’s more troublesome, I don’t quite understand it)
e. Any other combination (array array, etc.) is [not equal].

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template