1//ternary operator example:
2var isOpen = true; //try changing isOpen to false
3var welcomeMessage = isOpen ? "We are open, come on in." : "Sorry, we are closed.";
4
5
1// ternary operators are frequently used as a shorter cleaner if statement
2// condition ? exprIfTrue : exprIfFalse
3
4let age = 15;
5let canDrive = age >= 16 ? 'yes' : 'no';
6// canDrive will be 'no'
7// the condition will be age > 16 which is false so canDrive will equal exprIfFalse
8
9// this ternary is the same as this if else statement
10let age = 15;
11let canDrive;
12if (age >= 16) {
13 canDrive = 'yes';
14} else {
15 canDrive = 'no';
16}
17
18// canDrive will be 'no' because 15 is less than 16
19
20
1// Write your function here:
2
3const lifePhase = (age) => {
4 return age < 0 || age > 140 ? 'This is not a valid age':
5 age < 3 ? 'baby':
6 age < 13 ? 'child':
7 age < 20 ? 'teen':
8 age < 65 ? 'adult':'senior citizen';
9}
10
11
12
13
14 console.log(lifePhase(5))
1condition ? exprIfTrue : exprIfFalse
2
3//Example
4let age = 26;
5let beverage = (age >= 21) ? "Beer" : "Juice";
6console.log(beverage); // "Beer"