1condition ? doThisIfTrue : doThisIfFalse
2
31 > 2 ? console.log(true) : console.log(false)
4// returns false
1//ternary operator syntax and usage:
2condition ? doThisIfTrue : doThisIfFalse
3
4//Simple example:
5let num1 = 1;
6let num2 = 2;
7num1 < num2 ? console.log("True") : console.log("False");
8// => "True"
9
10//Reverse it with greater than ( > ):
11num1 > num2 ? console.log("True") : console.log("False");
12// => "False"
1let showme || "if the variable showme has nothing inside show this string";
2let string = condition ? 'true' : 'false'; // if condition is more than one enclose in brackets
3let condition && 'show this string if condition is true';
1var variable;
2if (condition)
3 variable = "something";
4else
5 variable = "something else";
6
7//is the same as:
8
9var variable = condition ? "something" : "something else";
1## Conditional (ternary) operator
2#1 one condition
3condition ? ifTrue : ifFalse;
4#2 Multi conditions
5condition1 ? value1
6 : condition2 ? value2
7 : condition3 ? value3
8 : value4;
1// example:
2age >= 18 ? `wine` : `water`;
3
4// syntax:
5// <expression> ? <value-if-true> : <value-if-false>