if ternario javascript 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: expresiones ternarias javascript

var condition = false;
var result1 = "First result";
var result2 = "Second result";
var result = condition ? result1 : result2;
//result1 will be assigned to result if condition is true.
//If not, result2 will be assigned

Example 3: examples of Conditional Operator js

var age = 19;
var canDrive = age > 16 ? 'yes' : 'no';

Example 4: ternary operator if javascript

condition ? exprIfTrue : exprIfFalse