Difference between == and === in javascript

  For comparison operators in JavaScript, you may use "==" more, and many people may be unfamiliar with "===". === means identity. First, compare whether the data types of the variables on both sides are equal, and then compare whether the values ​​of the variables on both sides are equal; == means equality, that is, only compare whether the values ​​of the variables on both sides are equal.

 

1. "===" first calculates the value of its operand, and then compares the two values, the comparison process does not have any type conversion

1. If two value types are not the same, they are not equal.

2. If both values ​​are null or both are undefined, they are not equal.

3. If both values ​​are boolean true or false, they are equal.

4. If one of the values ​​is NaN, or if both values ​​are NaN, they are not equal. NaN is not equal to any other value, including itself! ! ! Judging whether x is NaN by x!==x, the value of this expression is true only when 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 the two values ​​are strings and the 16-digit numbers in the corresponding bits are exactly the same, they are equal. If they are different in length or content, they are not equal. Two strings may mean exactly the same and display the same hand characters, but have 16-bit values ​​in different encodings. JavaScript doesn't standardize Unicode conversions, so strings like this don't compare equal via the "===" and "==" operators.

7. Two references are equal if they refer to the same object, array, or function. If they point to different objects, they are not equal. Although both objects have exactly the same properties.

E.g



var param1= '1', param2 = '1' ; 
param1 === param2; // both type and value are equal true 
var param3 = 1; 
param1 === param3; // type is not equal and value is equal false 
var param4 = 2; 
param1 === param4; //type and value are not equal false


var param1 = null, param2 = undefined; 
param1 === param2; //false

 

Second, the equality operator "==" If the two operands are not the same type, then the equality operator will try some type conversion, and then compare

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

2. If one value is a number and the other is a string, first convert the string to a number, and then use the converted value to compare.

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

4. If one value is an object and the other is a number or string, convert the object to the original value and then compare. Objects are converted to primitive values ​​by the toString() method or the valueOf() method. The built-in classes in the core of JavaScript first try to use valueOf() and then toString(), except for the date class, which only uses toString() conversion. Objects that are not in the core of the JavaScript language are converted to primitive values ​​through methods defined in their respective implementations.

5. The comparisons between other different types are not equal.

E.g

var param1= '1', param2 = '1' ; 
param1 == param2; // both type and value are equal true 
var param3 = 1; 
param1 == param3; // type is not equal and value is equal true 
var param4 = 2; 
param1 == param4; //type and value are not equal false

var param1 = null, param2 = undefined; 
param1 === param2; //true

 

Original URL: http://www.cnblogs.com/baizhanshi/p/4604257.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326377420&siteId=291194637