+ 9
What is ternary operator?
3 Respostas
+ 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
+ 5
This is a shorthand for a conditional expression:
condition? expression1: expression2;
Or
if(condition) expression1 else expression2