一、若是类型不一样,则不相等javascript
二、若是两个都是数值,而且是同一个值,则相等。但例外的是 NaN !== NaN(能够经过 isNaN() 来判断是否为 NaN)java
三、若是两个都是字符串,每一个位置的字符都同样,那么相等,不然不相等数组
四、若是两个值都为true,或者都为false,那么相等函数
五、若是两个值都引用同一个对象或函数,那么相等ui
六、若是两个值都是null,或者都是undefined,那么相等spa
一、同类型比较,进行 === 比较指针
二、若是两个值类型不一样,则根据以下规则进行类型转换再进行比较:code
1)若是一个是字符换,一个是数字,则将字符串转换成数字,再进行值比较
2)若是任一值是 true,把它转换成 1 再比较;若是任一值是 false,把它转换成 0 再比较。
3)若是一个是对象,另外一个是基础类型(数值或字符串),则把对象转换成基础类型的值再比较。
对象转换成基础类型,利用它的 toString 或者 valueOf 方法。
4)高级类型与高级类型,进行指针地址比较
5)若是一个是null,一个是undefined,那么相等
复制代码
一、any -> boolean
number -> boolean 除了 0, -0, NaN 都为 true
string -> boolean 除了空串,都为true
undefined、null -> boolean 为 false
引用类型 -> boolean 都为 true, [] -> true, {} -> true
二、any -> string
number -> string 5 -> '5'
boolean -> string true -> 'true'
数组 -> string [1,2] -> '1,2'
对象 -> string {...} -> '[object,Object]'
二、any -> number
string -> number '' -> 0, '1' -> 1, 'a' -> NaN
boolean -> number true -> 1, false -> 0
数组 -> number [] -> 0, [1] -> 1, [1,2] -> NaN (空数组转为0,存在一个元素且元素为数字转为数字,其余状况转为NaN)
null -> number 0
undefined -> number NaN
除了数组的引用类型 -> number 都为NaN, {} -> NaN
复制代码
// number -> boolean
console.log(Boolean(0)); // false
console.log(Boolean(-0)); // false
console.log(Boolean(NaN)); // false
console.log(Boolean(1111)); // true
// string -> boolean
console.log(Boolean('')); // false
console.log(Boolean('11')); // true
// undefined、null -> boolean
console.log(Boolean(undefined)); // false
console.log(Boolean(null)); // false
// 引用类型 -> boolean
console.log(Boolean([])); // true
console.log(Boolean({})); // true
console.log(Boolean([111])); // true
复制代码
// number -> string
console.log(String(0)); // '0'
console.log(String(111)); // '111'
// boolean -> string
console.log(String(true)); // 'true'
console.log(String(false)); // 'false'
// 数组 -> string
console.log(String([])); // ''
console.log(String([1, 2])); // '1,2'
console.log(String(['1', '2', '3'])); // '1,2,3'
// 对象 -> string
console.log(String({})); // '[object Object]'
console.log(String({ a: 'a' })); // '[object Object]'
复制代码
// string -> number
console.log(Number('')); // 0
console.log(Number('11')); // 11
console.log(Number('a')); // NaN
// boolean -> number
console.log(Number(true)); // 1
console.log(Number(false)); // 0
// 数组 -> number
console.log(Number([])); // 0
console.log(Number([111])); // 111
console.log(Number([111, 222])); // NaN
console.log(Number(['a'])); // NaN
// undefined、null -> number
console.log(Number(undefined)); // NaN
console.log(Number(null)); // 0
// 除了数组的引用类型 -> number
console.log(Number({})); // NaN
console.log(Number(function () { })); // NaN
复制代码
console.log('111' == 111); // true
console.log('a' == 1); // false
复制代码
console.log(null == undefined); // true
复制代码
console.log(true == 1); // true
console.log(false == 0); // true
复制代码
console.log({} == {}); // false
const obj1 = {};
const obj2 = obj1;
console.log(obj2 == obj1); // true
console.log([] == []); // false
const arr1 = [];
const arr2 = arr1;
console.log(arr1 == arr1); // true
复制代码
// 等号右边:
// ![] -> false -> 0 (说明: 全部引用类型转化为 boolean 都为 true, 所以 [] -> true, ![] -> false,
// 若是任一值是 true, 把它转换成 1 再比较。所以 false -> 0 )
// 等号左边:
// [] -> 0 (说明:因为等号右边是number类型, 所以须要转化为number类型)
console.log([] == ![]); // true
// 等号右边:
// !{} -> false -> 0 (说明: 全部引用类型转化为 boolean 都为 true, 所以 {} -> true, !{} -> false,
// 若是任一值是 true, 把它转换成 1 再比较。所以 false -> 0 )
// 等号左边:
// {} -> NaN (说明:因为等号右边是number类型, 所以须要转化为number类型)
console.log({} == !{}); // false
// 等号右边: 字符串: '111,222'
// 等号左边: [111, 222] -> '111,222' (说明:因为等号右边是 string 类型, 所以须要转化为 string 类型)
console.log([111, 222] == '111,222') // true
// 等号右边: 数字: 111
// 等号左边: [111, 222] -> NaN (说明:因为等号右边是 number 类型, 所以须要转化为 number 类型)
console.log([111, 222] == 111) // false
// 等号右边: 字符串: '111'
// 等号左边: [111, 222] -> '[object Object]' (说明:因为等号右边是 string 类型, 所以须要转化为 string 类型)
console.log({} == '111') // false
console.log({} == '[object Object]') // true
复制代码
我的博客: www.cxdsimple.com/对象