if...else 문을 단순화 하기 위해 삼항연산자(Ternary Operator)를 이용할 수 있다.
let isNightTime = true;
if (isNightTime) {
console.log('Turn on the lights!');
} else {
console.log('Turn off the lights!');
}
를
isNightTime ? console.log('Turn on the lights!')
: console.log('Turn off the lights!');
로 바꿀 수 있다.
조건이 ?
앞에 위치한다. ?
뒤에 두개의 표현이 오고, 두 부분이 :
로 분리된다.
조건문이 false로 평가되면 두번째 표현이 출력된다.