ternary operators in js code example
Example 1: js ternary
condition ? ifTrue : ifFalse
Example 2: javascript ternary
age >= 18 ? `wine` : `water`;
Example 3: ternary function javascript
var variable;
if (condition)
variable = "something";
else
variable = "something else";
var variable = condition ? "something" : "something else";
Example 4: ternary operator javascript
let age = 15;
let canDrive = age >= 16 ? 'yes' : 'no';
let age = 15;
let canDrive;
if (age >= 16) {
canDrive = 'yes';
} else {
canDrive = 'no';
}
Example 5: ternary operator javascript
condition ? exprIfTrue : exprIfFalse
let age = 26;
let beverage = (age >= 21) ? "Beer" : "Juice";
console.log(beverage);