0
Ternary Operator
I'm yet to understand this code: var age = 42; var isAdult = (age < 18) ? "Too young": "Old enough"; document.write(isAdult); < means less than > means greater than if it is true, why is the statement below correct? This is because age(45) is greater than 18. Why will the result be Old enough and not Too young?
3 odpowiedzi
+ 2
Thanks man.
+ 2
The ternary operator evaluates an expression like the if-else statement.
if(exp){
//code to run
} else{
//code to run
}
Just in the same vein the ternary operator performs the same operation on a line:
exp? TRUE : FALSE.
0
Basically, it is like a simple if-else situation, abbreviated.
String x;
if(2>1)
x = "two";
else
x = "one";
This translates to:
String x;
x = 2>1? "Two": "one";