In the application of JavaScript, it is often encountered to determine whether two objects/values are equal. Some indicate that they look the same, but they are not the same. There are some special cases that we need to identify.
Reference type
They are all reference types, and the stored space is allocated from the heap. The value at the variable is a pointer to the memory where the object is stored.
Two objects with the same properties.
var a = { x : 1 }var b = { x : 1 }a === b // false 两个单独的对象永不相等a == b // false 两个单独的对象永不相等
They are actually new Object()
shorthand. When the variable A is initialized, an object value is assigned, which opens up a new space in the heap.
Initializing variable B also opens up a new space. Two variables, the memory address pointed to is not the same.
Two separate empty arrays.
var a = [], b = []a === b // false 两个单独的数组永不相等。a == b // false 两个单独的数组永不相等。
The above two examples have the same principle.
The original type determines whether the value of type number is Nan
ECMAScript (ES6) contains the Number.isnan () function.
Number.isNaN(x)
to detect if the variable x is a non-numeric value. However, in the absence Number.isNaN
of a function, it (x != x)
is more reliable to detect if the variable x is through an expression NaN
.
String type
String type, I do not currently find the same literal, will not be equal to the case.
Symbol type
const s1 = Symbol() const s2 = Symbol()s1 == s2 // falses1 === s2 // false
The symbol () function returns the value of the symbol type, is unique, is not equal, and has a static property and a static method.
Finish
"X! = X" in JavaScript