+ 5
What is conditional (ternary) operator?
2 Antworten
+ 10
The ternary operator is basically a simpler and more efficient of creating an if/else statement. The basic syntax of one may look like this:
condition ? value1:value2
Using the example above, if the condition returns true, then value1 will be used in whatever way needed. If the condition returns false, then value2 will be used. This can be useful when trying to set a variable under a certain condition:
var x = y > 18 ? "Accepted":"Denied"; //The variable x will be set to either "Accepted" or "Denied", depending on whether or not the condition returns true
0
OK