Equal signs and non-equal signs of JavaScript equality Operators

Source: Internet
Author: User

Determine whether two variables are equalProgramVery important computation in design. This operation is quite simple when processing the original value, but it is a little complicated to design the object. Javascript provides two sets of operators to handle this problem. equal signs and non-equal signs are used to process the original values. Full and non-full signs are used to process objects.

In JavaScript,The equal sign is represented by the double equal sign (= ).,Returns true if and only if the number of two operations is equal..The non-equal sign is an exclamation point plus an equal sign (! =), Returns true if and only if the number of two operations is not equal. To determine whether two arithmetic operations are equal, both operators perform type conversion.

The basic rules for executing type conversion are as follows:

If a number is a Boolean value, convert it to a numeric value before checking equality. Convert false to 0, and true to 1.

If one operation is a string and the other is a number, convert the string to a number before checking for equality.

If one operation is an object and the other is a string, you must convert the object to a string (call the tostring () method) before checking equality ).

If one operation is an object and the other is a number, convert the object to a number before checking for equality.

During comparison, this operator also complies with the following rules:

The null and undefined values are equal.

When equality is checked, null and undenfined cannot be converted into other values.

If the number of operations is Nan, the equal sign returns false, and the non-equal sign returns true. Important: even if the number of connected operations is Nan, the equal sign still returns false, because according to the rule, Nan is not equal to Nan.

If both operations are objects, their reference values are compared. If the number of two operations points to the same object, the equal sign returns true; otherwise, the number of two operations varies.

The comparison below lists some special cases and their results:

Null = undefined // The result is: True

"Nan" = Nan // The result is: false.

5 = Nan // The result is: false

Nan = Nan // The result is: false.

Nan! = Nan // The result is: True.

False = 0 // The result is: True.

True = 1 // The result is: True

True = 2 // The result is: false

Undefined = 0 // The result is: false

Null = 0 // The result is: false

"5" = 5 // The result is: True

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.