1. 严格相等 ===(严格相等比较的是两个值是否相等,两个被比较的值在比较前都不进行隐式转换(即不转换数据类型),所以当两个值当数据类型不一致时,是不相等的)
console.log(0 === 0)// true
console.log(+0 === -0)// true
console.log(0 === -0)// true
console.log(0 === +0)// true
console.log(0 === new Number(0))// false
console.log(new Number(0) === new Number(0))// false 可以使用typeof查看两者的区别
console.log(Number(0) === Number(0))// true
console.log(0 === '0')// false
console.log(0 === '')// false
console.log(0 === null)// false
console.log(0 === undefined)// false
console.log(0 === new String())// false
console.log('0' === '0')// true
console.log('0' === new String(0))// false
console.log(new String(0) === new String(0))// false
console.log(String(0) === String(0))// true
console.log(null === null)// true
console.log(null === undefined)// false
console.log(undefined === undefined)// true
console.log(NaN === NaN)// false
console.log(0 === false)// false
console.log(false === false)// true
console.log({} === {})// false
console.log({} === new Object())// false
console.log(new Object() === new Object())// false
console.log(Object() === Object())// false
console.log([] === [])// false
console.log([] === new Array())// false
console.log(new Array() == new Array())// false
console.log(Array() == Array())// false
console.log(Symbol() === Symbol())// false (ES6语法)
console.log(Symbol('test') === Symbol('test'))// false (ES6语法)
建议不要使用new 新建对象
2. 非严格相等 ==(非严格相等比较是两个值是否相等,两个被比较的值,在被比较前会被转为相同的数据类型)
console.log(0 == 0)// true
console.log(+0 == -0)// true
console.log(0 == -0)// true
console.log(0 == +0)// true
console.log(0 == new Number(0))// true
console.log(new Number(0) == new Number(0))// false
console.log(Number(0) === Number(0))// true
console.log(0 == '0')// true
console.log(0 == '')// true
console.log(0 == null)// false
console.log(0 == undefined)// false
console.log(0 == new String())// true
console.log('0' == '0')// true
console.log('0' == new String(0))// true
console.log(new String(0) == new String(0))// false
console.log(String(0) == String(0))// true
console.log(null == null)// true
console.log(null == undefined)// true
console.log(undefined == undefined)// true
console.log(NaN == NaN)// false
console.log(0 == false)// true
console.log(false == false)// true
console.log({} == {})// false
console.log({} == new Object())// false
console.log(new Object() == new Object())// false
console.log(Object() == Object())// false
console.log([] == [])// false
console.log([] == new Array())// false
console.log(new Array() == new Array())// false
console.log(Array() == Array())// false
console.log(Symbol() == Symbol())// false (ES6语法)
console.log(Symbol('test') == Symbol('test'))// false (ES6语法)
3. Object.is() (ES6语法,用来比较两个值是否严格相等,与严格相等的行为基本一致,不同之处:+0不等于-0;NaN等于NaN)
console.log(Object.is(+0, -0))// false
console.log(Object.is(0, -0))// false
console.log(Object.is(+0, 0))// true
console.log(Object.is(NaN, NaN))// true