Home > Web Front-end > JS Tutorial > body text

Compare the usage differences between == and ===

巴扎黑
Release: 2017-07-24 14:00:06
Original
2467 people have browsed it


1. For basic types such as string and number, there is a difference between == and ===
1) Comparison between different types , == compares "values ​​converted into the same type" to see if the "values" are equal. === If the types are different, the result is unequal
2) Compare the same type, directly perform the "value" Comparing, the results are the same

2. For advanced types such as Array and Object, there is no difference between == and ===
Compare "pointer address"

3. There is a difference between basic types and advanced types, == and ===
1) For ==, convert the advanced type into a basic type and perform "value" comparison
2) Because the types are different, the result of === is false

Sometimes you will see three equal signs (===) used when judging whether two objects are equal. What is the difference between it and two equal signs (==)? Woolen cloth? To put it simply, when using "==", if the types of both sides are different, the js engine will convert them into the same type and then compare them, while "===" will not perform type conversion, so when the two sides do not belong to the same Type, definitely not equal. For example:

var a = 0, b = '0';

alert((a == b) + '--' + (a === b))
Copy after login

The result seen at this time is "true–false".

=== Judgment rules

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

== Judgment rules:

  1. If the two value types are the same, perform === comparison.

  2. If two value types are different, they may be equal. Perform type conversion and then compare according to the following rules:

  • If one is null and the other is undefined, then [equal].

  • If one is a string and the other is a numerical value, convert the string into a numerical value and then compare.

  • If any value is true, convert it to 1 and compare; if any value is false, convert it to 0 and compare.

  • 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, let's say (it's more troublesome, I don't quite understand)

  • Any other combination is [not equal].

Special attention needs to be paid to the conversion of true and false, for example:

alert(true == 1);  //ture
alert(true == 2); //false, true会转换成number,也就是1,当然 1 不等于 2
//可以使用 !! 来把一个数据类型转换为boolean型
alert(true == !!2) //true,!2 === false !(!=2) = !false = true
Copy after login

In addition, in js, if a variable is used in a logical operation, then the variable is When there is no initial value or its value is 0, -0, null, "", false, undefined or NaN, its value is false. Otherwise, its value is true.

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!