Multiple Conditional (Ternary) Operators

Tell us what’s happening:
It works with and without parentheses around num > 0 , whats the difference. Examples online are also mostly without parentheses.

   **Your code so far**

function checkSign(num) {
return num > 0 ? "positive"
: num < 0 ? "negative"
: "zero";
}

checkSign(10);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0

Challenge: Use Multiple Conditional (Ternary) Operators

Link to the challenge:

1 Like

In this specific example, nothing. You’ve got a simple boolean condition before the question mark, which is exactly what the ternary operator expects. Putting parens around that condition doesn’t change that, and it doesn’t provide any added help to JS, it would be purely for the person reading the code. I’m sure you can find arguments on both sides of the issue as to whether to always use parens. And I’m sure you could also find arguments as to whether you should chain ternary operators like that. If you work for an employer that has a style guide in place then those decisions will most likely be made for you.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.