The difference between == and === in JavaScript: == performs a loose equality comparison, forcing the values to be converted to the same type before comparison; === performs a strict equality comparison, comparing not only values but also types, different Type values are always unequal.
The difference between == and === in JavaScript
In JavaScript, ==
and ===
are two different equality operators that are used to compare two values for equality.
#==
(Loose Equality) The
==
operator performs a loose equality comparison, which means it will Try casting two values to the same type and then compare their values. Here are some examples of the behavior of the ==
operator:
<code>1 == "1" // true 0 == false // true [] == "" // true</code>
===
(strict equality)
# The ##=== operator performs a strict equality comparison, which means that it not only compares the values of two values, but also their types. The
=== operator will always return
false if the two values are not of the same type. Here are some examples of how the
=== operators behave:
<code>1 === "1" // false 0 === false // false [] === "" // false</code>
Usage Guidelines
In general, it is recommended to use strict in JavaScript code Equality operator===. This is because the behavior of the
== operator can lead to unexpected results, especially when different types of values are involved.
==. For example, when you just want to compare the textual representation of two values and don't care about their types.
The above is the detailed content of The difference between == and === in js. For more information, please follow other related articles on the PHP Chinese website!