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?

30th Jun 2018, 9:11 PM
NobleSoul
NobleSoul - avatar
3 ответов
+ 2
Thanks man.
30th Jun 2018, 9:19 PM
NobleSoul
NobleSoul - avatar
+ 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.
30th Jun 2018, 9:23 PM
Adedokun Yinka Enoch
Adedokun Yinka Enoch - avatar
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";
11th Sep 2019, 5:35 AM
A Darren Cruz
A Darren Cruz - avatar