Hello everyone, I am 梅巴哥er
, this article introduces an interesting little knowledge point.
When looking at the basic types of data, this question comes to mind
null, undefined, NaN, Infinity的值是否相等或全等
:?
Later, I saw other people asking, so I tested it with the code:
console.log(null == undefined)
console.log(null === undefined)
console.log(null === null)
console.log(NaN == null)
console.log(NaN === NaN)
console.log(Infinity + 1 !== Infinity)
Don't look down yet, guess what the output will be?
// 是这样吗?
true
false
true
false
false
false
- The first one is easy to understand, that's how JS is defined. The result must be true
- The second one is also easy to understand, the two are different data types, naturally false
- The third is the same data type and the same value.
- These latter values are all different.
By the way, the difference between null and undefined:
- Null is generally used to initialize data, such as setting a certain data to be empty
var arr = null
. Indicates that this value "should be empty" - undefined is undefined empty, that is to say "should have a value" here, but the undefined value causes the result to be empty. such as
var a; console.log(a) // undefined
the above.