ps:记录自己不是特别清楚的部分
加号
tip1、如果两个操作数都是字符串,就拼接;
tip2、如果有一个是字符串,则另外一个操作数转换成字符串,然后在拼接
tip3、如果一个操作数是对象、数值或者boolean,则调用它们的toString()方法,在按照上面的规则来;
tip4、如果有一个操作数为null,而另外一个为数值,则把null转换成0,在做运算;
tip5、如果有一个操作数为undefined,另一个为数值,则返回NaN
tip6、如果有一个操作数为null,而另外一个为非数值,则null调用String()方法转成字符串,在做拼接;
tip7、如果有一个操作数为undefined,另一个为非数值,则undefined调用String()方法转成字符串,在做拼接;
举例子:
console.log(5+undefined)//NaN console.log("5"+undefined)//'5undefined' console.log(5+null)//5 console.log('5'+null)//'5null'
减号
tip1、如果一个操作数为字符串、boolean、null、undefined,则先调用Number()将其转换为一个数值,在做减法运算,如果转换的结果是非数值的,则最终结果为NaN;
tip2、如果一个操作数是对象,则调用其valueOf()方法,如果转换的结果为非数值的 ,则最终结果为NaN; 如果对象没有valueOf() 方法,则调用toString()方法,转换成字符串,在将字符串转成数值,在做运算;
大概举几个例子:
console.log(5-undefined)//NaN console.log("5"-undefined)//NaN console.log(5-null)//5 console.log('5'-null)//5