Equality and inequality in Javascript

Source: Internet
Author: User

In JavaScript, you can use = = to compare two data for equality, and if the two data types are different, the conversion will be compared after the conversion, with the following rules:

L If the type of one of the operands is Boolean, first convert it to a number type, false to 0, and true to 1.

If the type of one of the operands is a string and the other is a numeric type, then the string is converted to a number for comparison.

L If the type of one of the operands is a string and the other is of type object, then the object's ToString method is called after the string is compared.

L If the type of one of the operands is a numeric type and the other is of type object, then a numeric comparison is made after the objects are converted to numeric values.

Some special comparisons are made in the following provisions:

L null and undefined are equal.

L null and undefined are not converted to any other type

L If the result of any one operation is NaN, then the equality comparison returns false, and the inequality comparison returns true. Note that even if the two operands are Nan, the returned result is false, that is, Nan is not equal to Nan.

L If the two operands are objects, then compare the values they reference, if the same object is referenced, then return true, otherwise, return false.

Alert (Null = = undefined); True

alert (undefined = = null); True

Alert (true = = 1); True

Alert (false = = 0); True

Alert (true = = 2); False

var obj = {};

alert (= = obj); False

The exact same comparison = = = And not exactly equal!==

The exact same comparison is used to compare whether the conversion is not equal, for example:

var a = "100";

var b = 100;

Alert (A = = B); True

Alert (A = = B); False

The = = Comparison returns true because "100" is first converted to the number 100 and then compared with the number 100, resulting in equality.

= = = The comparison will return false, because the string "100" is not converted, and the number 100 is not equal.

!== are used to compare whether they are not equal in the case of non-conversions.

Alert (A! = b); False

Alert (a!== b); True

Equality and inequality in Javascript

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.