您的位置:首页 > 其它

4.运算符的优先级

2020-08-21 18:07 127 查看

4.运算符的优先级

var val = 'smtg';
console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');  //Something

这个题最容易犯的错误是‘Value is Something’
判断优先级最高的是括号内部的

var val = 'smtg';
console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');  //Something//判断优先级最高的是括号内部的
'Value is ' + (val === 'smtg') ? 'Something' : 'Nothing'
=>等价于'Value is ' + true? 'Something' : 'Nothing'
//+的优先级高于?的
'Value is ' + true? 'Something' : 'Nothing'
=> 'Value is true' ? 'Something' : 'Nothing'
//因为'Value is true' != null,其布尔值为true
'Value is true' ? 'Something' : 'Nothing'  //Something

//综上所述
'Value is ' + (val === 'smtg') ? 'Something' : 'Nothing' //Something
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: