if statement shorthand code example
Example 1: conditional operator
var age = 26;
var beverage = (age >= 21) ? "Beer" : "Juice";
console.log(beverage);
Example 2: javascript if shorthand
condition ? doThisIfTrue : doThisIfFalse
1 > 2 ? console.log(true) : console.log(false)
Example 3: short for if
#include <iostream>
a == 1 ? std::cout << "1" : (a == 2 ? std::cout << "2" : a = 3);
if(a == 1)
{
std::cout << "1";
}
else if (a == 2)
{
std::cout << "2";
}
else
{
a = 3;
}
Example 4: if and else shorthand
let points = 70;
let result;
if(marks >= 50){
result = 'Pass';
}else{
result = 'Fail';
}
let points = 70;
let result = marks >= 50 ? 'Pass' : 'Fail';