JavaScript #14 ~ The Conditional (Ternary) Operator

HJ's Coding Journey·2021년 6월 26일
0

[Learn] JavaScript

목록 보기
15/20

Besides the if/else statement and the switch statement, there is also the conditonal (ternary) operator. this is known as 'ternary' because there is a three part process in creating a conditional code. The functionality is similar with both if/else statement and the switch statement. The only difference is that the conditional operator is much simpler and shorter to write. We can literally fit all the code within a single variable.

const example = age > 18 ? console.log('1st Condition') : ('2nd Condtion');

We can even write this code using template literals.

console.log(`I like to drink ${age >= 18 ? 'wine' : 'water'}`);

However, this does not mean that we can ignore the if/else statement or the switch statement. Different methods are used for different situtations. So it is good to remember all 3 ways of creating conditional codes 😀.

profile
Improving Everyday

0개의 댓글