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"
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";
1String year = credits < 30 ? "freshman" : credits <= 59 ? "sophomore" : credits <= 89 ? "junior" : "senior";
1//ternary operator example:
2var isOpen = true; //try changing isOpen to false
3var welcomeMessage = isOpen ? "We are open, come on in." : "Sorry, we are closed.";
4
5