convert normal if condition to ternary operator code example
Example 1: javascript ternary operator
//ternary operator syntax and usage:
condition ? doThisIfTrue : doThisIfFalse
//Simple example:
let num1 = 1;
let num2 = 2;
num1 < num2 ? console.log("True") : console.log("False");
// => "True"
//Reverse it with greater than ( > ):
num1 > num2 ? console.log("True") : console.log("False");
// => "False"
Example 2: javascript ternary operator
//ternary operator example:
var isOpen = true; //try changing isOpen to false
var welcomeMessage = isOpen ? "We are open, come on in." : "Sorry, we are closed.";
Example 3: convert normal if condition to ternary operator
const age = 19;
if(age >= 18) {
const canVote = true;
} else {
const canVote = false;
}
if (age>18) {
console.log("Mozete glasati")
}
Example 4: convert normal if condition to ternary operator
const age = 19;
if(age >= 18) {
const canVote = true;
} else {
const canVote = false;
}
if (age>18) {
console.log("Mozete glasati")
}