簡寫規則
- 不用寫 if
- 條件句不用括起來
- 【?】若為真,後面放條件為真的動作
- 【:】若為否,後面放條件為否的動作
範例
//原本的複雜版寫法
const a=5;
const b=10;
var result;
if(a<b){
result="a great than b";
}else{
result="a less than b";
}
console.log(result); //"a great than b"
//簡寫版
a<b ? result2="a great than b" : result2="a less than b";
console.log(result2); //"a great than b"