js原始值与对象的相等比较

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>原始值与对象的相等比较</title>
</head>
<body>
<script>

let a = [];
let b = “0”;
// 1
console.log(a0); // true
// 2
console.log(a!a);// true
// 3
console.log(ab); // false
// 4
console.log(b0); // 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,其它值转为布尔型都为true。
*/

console.log(null == 0); // false
console.log(null<0); // false
console.log(null<=0); // true
// 要比较相等性之前,不能将null和undefined转换成其他任何值。就是undefined和null与其他数在进行相等判断时不进行类型转换。
// null == undefined,这个是true,但是关系运算符可以转换。

// 1、如果x不是正常值(比如抛出一个错误),中断执行。

// 2、如果y不是正常值,中断执行。

// 3、如果Type(x)与Type(y)相同,执行严格相等运算x === y。

// 4、如果x是null,y是undefined,返回true。

// 5、如果x是undefined,y是null,返回true。

// 6、如果Type(x)是数值,Type(y)是字符串,返回x == ToNumber(y)的结果。

// 7、如果Type(x)是字符串,Type(y)是数值,返回ToNumber(x) == y的结果。

// 8、如果Type(x)是布尔值,返回ToNumber(x) == y的结果。

// 9、如果Type(y)是布尔值,返回x == ToNumber(y)的结果。

// 10、如果Type(x)是字符串或数值或Symbol值,Type(y)是对象,返回x == ToPrimitive(y)的结果。

更多内容请见原文,原文转载自:http://www.mark-to-win.com/tutorial/50907.html

posted @ 2022-06-28 19:12  忘川信使  阅读(27)  评论(0编辑  收藏  举报