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

What is ternary operator?

21st Mar 2021, 2:34 AM
๐ŸŒ๐ŸŒŽ๐ŸŒแด€ษชแด๐ŸŒŽ๐ŸŒ๐ŸŒแดตโฟแตƒแถœแต—โฑแต›แต‰
๐ŸŒ๐ŸŒŽ๐ŸŒแด€ษชแด๐ŸŒŽ๐ŸŒ๐ŸŒแดตโฟแตƒแถœแต—โฑแต›แต‰ - avatar
2 Answers
+ 8
it is basically a shortened syntax for an if else statement and has the following syntax condition ? expression1 : expression2 if the condition is truthy then expression1 is returned otherwise expression 2. Example let a = 5 let b = a > 2 ? 3 : 2 let c = a * 10 < 20 ? 4 : 6 since a > 2 is truthy, b will have the value 3 since a*10<20 is not truthy, c will have the value 6
21st Mar 2021, 2:40 AM
John Doe
+ 5
This is a shorthand for a conditional expression: condition? expression1: expression2; Or if(condition) expression1 else expression2
21st Mar 2021, 2:38 AM
Solo
Solo - avatar