三目运算符 ?:
1 | if (hasMoney) { |
1 | hasMoney ? console.log('周末嗨翻天') : console.log('周末睡一天'); |
1 | let weekendPlan = hasMoney ? '周末嗨翻天' : '周末睡一天'; |
逻辑与操作符 &&
1 | if (hasMoney) { |
1 | hasMoney ? console.log('周末嗨翻天') : undefined; |
1 | hasMoney && console.log('周末嗨翻天'); |
1 | true && console.log('It is true'); // It is true |
逻辑或操作符 ||
1 | // 当自身为undefined时,赋值为0,否则还是赋值为自身 |
1 | val = val || 0; |
1 | // ES5设置函数默认值 |
按位取反操作符 ~
1 | let arr = ['we', 'are', 'the', 'BlackGold', 'team']; |
1 | let arr = ['we', 'are', 'the', 'BlackGold', 'team']; |
1 | let arr = ['we', 'are', 'the', 'BlackGold', 'team']; |
1 | let arr = ['we', 'are', 'the', 'BlackGold', 'team']; |
1 | console.log(~-1); // 0 转换为Boolean值即为false |