Home >Web Front-end >JS Tutorial >Are null and undefined equal in js?
null and undefined are not equal in JavaScript for the following reasons: null represents an empty object or a non-existent value, while undefined represents an unassigned variable or property. After the == operator converts null and undefined to numbers, null is 0 and undefined is NaN. NaN is not equal to any number, including itself, so null and undefined are not equal under the == operator.
Are null and undefined equal in js?
No, null and undefined are not equal in js.
Detailed description:
null is a special value in JavaScript, representing an empty object or a non-existent value. undefined is also a special value, indicating that a variable or property has not been assigned a value.
In JavaScript, the == operator checks whether two values are equal. However, the == operator has special handling for null and undefined. == automatically converts null and undefined to numbers and then compares the numbers for equality.
After conversion, null becomes 0 and undefined becomes NaN. NaN is a special value that represents not-a-number, which is not equal to any other number, including itself.
Therefore, the == operator returns false when comparing null and undefined.
Example:
<code class="js">console.log(null == undefined); // false console.log(null === undefined); // false</code>
The above is the detailed content of Are null and undefined equal in js?. For more information, please follow other related articles on the PHP Chinese website!