js ternary operator shorthand code example
Example 1: shorthand if statment in js
isLoggedIn ? "Logout" : "Login";
Example 2: js ternary
condition ? ifTrue : ifFalse
Example 3: javascript ternary
age >= 18 ? `wine` : `water`;
Example 4: ternary operator shorthand javascript
let startingNum = startingNum ? otherNum : 1
let startingNum = otherNum || 1
let startingNum = startingNum ? otherNum : 0
let startingNum = startingNum && otherNum
Example 5: 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 6: javascript ternary
condition ? doThisIfTrue : doThisIfFalse
1 > 2 ? console.log(true) : console.log(false)