The difference between = = and = = in JavaScript

Source: Internet
Author: User

For the comparison operator in JavaScript, we may use more "= =", for "= = =" Many people may be very unfamiliar. = = = = Identity, first compare the two sides of the variable data type is equal, followed by comparison of the two sides of the variable value is equal; = = equals is equal to only the value of the two variables.

the "= = =" First calculates the value of its operand, then compares the two values, and the comparison process does not have any type conversions

1, if the two value types are different, they are not equal.

2, if two values are null or both are undefined, they are not equal.

3, if two values are Boolean true or FALSE, then they are equal.

4, if one of the values is Nan, or two of the two values are Nan, they are not equal. Nan and any other values are not equal, including IT itself!!! By x!==x to determine if X is Nan, the value of this expression is true only if X is Nan.

5, if two values are numbers, and the values are equal, they are equal. If one is 0 and the other is-0, they are equally equal.

6. If two values are strings and the 16-bit numbers on the corresponding bits are equal, they are equal. They are not equal if they are of different lengths or content. Two strings may have exactly the same meaning and the displayed shot characters are the same, but with a different encoded 16-bit value. JavaScript does not standardize the conversion of Unicode, so strings like this are not equal to the results of comparisons with the "= = =" and "= =" operators.

7. If two reference values are the same as an object, an array, or a function, they are equal. If you point to different objects, they are unequal. Although two objects have exactly the same properties.

For example



var param1= ' 1 ', param2 = ' 1 ';
param1 = = = Param2; Both types and values are equal to True
var param3 = 1;
param1 = = = Param3; Types are unequal and numeric equals false
var param4 = 2;
param1 = = = Param4; Type and value are not equal false


var param1 = null, param2 = undefined;
param1 = = = Param2; False

Second, equality operator "= =" If the two operands are not of the same type, then the equality operator tries some type conversions and then compares

1. If one value is null and the other is undefined, then they are equal.

2. If one value is a number and the other is a string, the string is first converted to a number and then compared using the converted value.

3. If one of the values is true, convert it to 1 and then compare. If one of the values is false, the base is converted to 0 and then compared.

4. If one value is an object and the other value is a number or string, the object is converted to the original value and then compared. The object is converted to the original value by the ToString () method or the ValueOf () method. The built-in class of the JavaScript core first attempts to use valueof () and then attempts to use ToString (), except for the date class, which uses only the ToString () conversion. Objects that are not in the core of the JavaScript language are converted to the original values through the methods defined in their implementation.

5, the comparison between the other different types are not equal.

For example

var param1= ' 1 ', param2 = ' 1 ';
param1 = = param2; Both types and values are equal to True
var param3 = 1;
param1 = = Param3; Type not equal and numeric equal true
var param4 = 2;
param1 = = PARAM4; Type and value are not equal false

var param1 = null, param2 = undefined;
param1 = = = Param2; True

The difference between = = and = = 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.