js三元表达式

条件 ? true的时候执行 : false时候执行

const x = 20;
let answer;
if (x > 10) {
    answer = 'greater than 10';
} else {
    answer = 'less than 10';
}

简写为:

const answer = x > 10 ? 'greater than 10' : 'less than 10';
posted @ 2017-10-25 17:15  peiyao  阅读(924)  评论(0编辑  收藏  举报