Home > Article > Web Front-end > The difference between === and == in js
In JavaScript, the == operator performs a loose equality comparison (converting types and comparing values), while the === operator performs a strict equality comparison (directly comparing types and values). It is recommended to use the === operator to avoid unexpected results.
The difference between === and == in JavaScript
== and === operators are JavaScript Operator used to compare two values for equality. Although they look similar, there are some key differences between them.
== (Loose Equality) The
== operator performs a loose equality comparison, which means it attempts to convert two values to the same type, and then Check if they are equal. This can lead to unexpected results:
<code class="js">console.log(1 == "1"); // true console.log([] == 0); // true console.log(false == null); // false</code>
=== (Strict Equality) The
=== operator performs strict equality comparisons, which means it does not An attempt will be made to convert the value to the same type. Instead, it directly compares the type and value of the value itself:
<code class="js">console.log(1 === "1"); // false console.log([] === 0); // false console.log(false === null); // false</code>
Choose which operator to use
Which operator to use depends on what kind of comparison you wish to make:
In general, it is recommended to use the === operator for equality comparisons because it avoids unexpected results from loose equality.
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!