js原始值與對(duì)象的相等比較
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>原始值與對(duì)象的相等比較</title>
</head>
<body>
<script>
let a = [];
let b = "0";
// 1
console.log(a==0); // true
// 2
console.log(a==!a);// true
// 3
console.log(a==b); // false
// 4
console.log(b==0); // true
// eg 1
console.log(Number([])); // 0
// eg 2
console.log(Boolean([])); // true
// eg 3
console.log(String([])); // ''
// eg 4
console.log(Number(b)); // 0
/*
js假值:只有false、null、undefined、空字符、0和NaN,其它值轉(zhuǎn)為布爾型都為true。
*/
console.log(null == 0); // false
console.log(null<0); // false
console.log(null<=0); // true
// 要比較相等性之前,不能將null和undefined轉(zhuǎn)換成其他任何值。就是undefined和null與其他數(shù)在進(jìn)行相等判斷時(shí)不進(jìn)行類型轉(zhuǎn)換。
// null == undefined,這個(gè)是true,但是關(guān)系運(yùn)算符可以轉(zhuǎn)換。
// 1、如果x不是正常值(比如拋出一個(gè)錯(cuò)誤),中斷執(zhí)行。
// 2、如果y不是正常值,中斷執(zhí)行。
// 3、如果Type(x)與Type(y)相同,執(zhí)行嚴(yán)格相等運(yùn)算x === y。
// 4、如果x是null,y是undefined,返回true。
// 5、如果x是undefined,y是null,返回true。
// 6、如果Type(x)是數(shù)值,Type(y)是字符串,返回x == ToNumber(y)的結(jié)果。
// 7、如果Type(x)是字符串,Type(y)是數(shù)值,返回ToNumber(x) == y的結(jié)果。
// 8、如果Type(x)是布爾值,返回ToNumber(x) == y的結(jié)果。
// 9、如果Type(y)是布爾值,返回x == ToNumber(y)的結(jié)果。
// 10、如果Type(x)是字符串或數(shù)值或Symbol值,Type(y)是對(duì)象,返回x == ToPrimitive(y)的結(jié)果。
// 11、如果Type(x)是對(duì)象,Type(y)是字符串或數(shù)值或Symbol值,返回ToPrimitive(x) == y的結(jié)果。
// 12、返回false。
</script>
</body>
</html>
歡迎關(guān)注我的公眾號(hào),回復(fù)關(guān)鍵詞【電子書】,即可獲取近十幾本前端熱門電子書。更有精品文章等著你哦。
你還可以加我微信,小弟拉攏了很多IT大佬,創(chuàng)建了一個(gè)技術(shù)交流、文章分享群,歡迎你的加入。
作者:Vam的金豆之路
主要領(lǐng)域:前端開發(fā)
我的微信:maomin9761
微信公眾號(hào):前端歷劫之路