What is conditional (ternary) operator? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

What is conditional (ternary) operator?

21st Apr 2018, 3:39 PM
Ghaits Hafidz Putra Widodo
Ghaits Hafidz Putra Widodo - avatar
2 Answers
+ 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
21st Apr 2018, 3:47 PM
Faisal
Faisal - avatar
0
OK
21st Apr 2018, 11:27 PM
Aidoosolomon
Aidoosolomon - avatar