运算符

console.log(1"2"+"2"); //122
 
console.log(1+ +"2"+"2"); //32
 
console.log("A""B"+"2"); //NaN2
 
console.log("A""B"+2); //NaN

 


1.
 
console.log(1"2"+"2");
对于加法来说,如果只有一个操作数是字符串,则将另一个操作数也转换为字符串,然后将两者拼接,为122

2.
 
console.log(1+ +"2"+"2");
(+"2")应用了一元加操作符,一元加操作符相当于Number()函数,会将 (+"2")转换为2,1+2+"2"=32

3.
 
console.log("A""B"+"2");
在减法中遇到字符串和加法相反,调用Number()函数将字符串转换为数字,不能转换则返回NaN,此时运用加法规则,NaN+"2","2"是字符串,则将两者拼接。js没有ASCALL码

4.
 
console.log("A""B"+2);
这个与上面的不太相同,减法运算后依然为NaN,但是加号后面的为数字2,加法规则中,如果有一个操作数是NaN,则结果为NaN
=======================================================================================================================================================

console.log('Value is ' + (val != '0') ? 'define' : 'undefine'); // define
➕运算符优先级大于三元运算符

所以代码执行顺序是('Value is ' + (val != '0')) ? 'define' : 'undefine',而?前面的表达式运算结果为字符串'Value is true',它被转换为布尔值是true,所以打印出来的结果是字符串'define'。
原文地址:https://www.cnblogs.com/aixiuxiu/p/6520141.html