Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialanonymous123
5,794 PointsTernary Operator
function checkSign(num) {
return num > 0 ? "positive" : num < 0 ? "negative": "zero" ;
}
checkSign(10);
why should the return statement come before the ternary operators? why should it not be in front of each string?
1 Answer
Eric Butler
33,512 PointsHey ah11,
You have to read ternary operators in a special (kinda unintuitive) way. It's not like an if-else
block, where you have to write a whole statement inside each condition. Maybe think of it like a multi-step puzzle, or one of those choose-your-own-adventure stories. It's all one statement, but it can go a number of directions as it gets evaluated (in this case, there are actually 2 ternary operators combined with each other). Here's the statement as you wrote it:
function checkSign(num) {
return num > 0 ? "positive" : num < 0 ? "negative": "zero" ;
}
JS sees the return
directive, but then looks at what's next to figure out what to return. Instead of a concrete value, it finds an operation, and so it evaluates it:
num > 0
That's either true or false. JS then keeps going and realizes that that's not the end of the statement, it's actually a ternary operator, so it takes that first evaluation and applies it to the conditions. If the evaluation was true, "positive" is added to the return
statement. If it's false, it continues with what's after the first :
, sees that it's another operation, and so evaluates that:
num < 0
That's again either true or false. And again, JS sees that this is another ternary operator and so applies that evaluation to the next set of conditions. If it was true, "negative" is added to the return
statement. If it's false, "zero" is added to it.
JS has to go through that before it reaches the semicolon (;) character, which finally tells JS that that's the end of the statement, and so whatever value came out of those ternary operators is what is supposed to be returned.
If it helps, here's an easier/different version of a ternary operator:
var whatever = (7 + 2 > 8) ? "Yes, math works" : "No, math is broken";
The same logic applies:
- The statement says, "Hey JS, create a variable named 'whatever' and assign it a value."
- And JS says, "Okay, what value?"
- And the statement says, "Well, if 7 plus 2 is greater than 8, then assign it the value 'Yes, math works.' Otherwise, assign it the value of 'No, math is broken.'"
- JS says, "Okay, 7 plus 2 is 9, which is greater than 8, so 'whatever' will have the value 'Yes, math works.'"
Carl Evison
2,656 PointsCarl Evison
2,656 PointsBecause there can only be one outcome, they both can't be returned so the return statement returns what ever is given back from the conditional statement.