Home >Web Front-end >JS Tutorial >The difference between javaScript '==' and '==='

The difference between javaScript '==' and '==='

小云云
小云云Original
2018-03-19 09:42:101231browse

Both html and php have operators. For the comparison operators in JavaScript, the one that may be used more often is "==". Many people may be unfamiliar with "===", so what exactly do they have? What's the difference?

=== means identity, first compares whether the data types of the variables on both sides are equal, and then compares whether the values ​​of the variables on both sides are equal; == means equality, that is, only compares whether the values ​​of the variables on both sides are equal.

Difference:

==, When the value types on both sides are different, type conversion must be performed first and then compared.
==, no type conversion is done, different types must not be equal.

The following are explained separately:

Let’s talk about "===" first, this is relatively simple. The following rules are used to determine whether two values ​​​​are === equal:

1. If the types are different, they are not equal

2. If both are numeric values ​​and they 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 they are equal; otherwise, they are not equal.

4. If both values ​​are true or both are false, then they are equal.

5. If two values ​​refer to the same object or function, they are equal; otherwise, they are not equal.

6. If both values ​​are null or both are undefined, they are equal.
Let's talk about "=="
If 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, they are 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. The js core built-in class will try valueOf before toString;

e, and any other combination will not be equal.

Summary:

In other words, "==" is a comparison between values, and: "===" is not just It is a comparison between values, and also a comparison between types. Generally, when working on a project, we decide which one to use based on different businesses. Most developers are used to "==", which is not rigorous.

Related recommendations:

Detailed explanation of the double equal sign (==) implicit conversion mechanism in Javascript

if and switch, == and === Detailed explanation of the difference and connection examples

PHP’s == operator string comparison usage and its side effects Detailed explanation


The above is the detailed content of The difference between javaScript '==' and '==='. For more information, please follow other related articles on the PHP Chinese website!

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