Use the Conditional (Ternary) Operator Error or Not?

Tell us what’s happening:

Hi every ! just noticed something in this question : function findGreater(a, b) {
return a > b ? “a is greater” : “b is greater”;

Here you can see that “a” is greater and “b” as well. Can anyone clarify me why ? or its just a mistake.

Your code so far

function checkEqual(a, b) {
  return a === b ? true : false;

checkEqual(1, 2);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1 Safari/605.1.15.

Link to the challenge:

With the ternary operator the expression (in this case a> b or a===b) is either true or false. If its true, the first value before the colon is returned, if false the second value.

So by saying return a > b ? “a is greater” : “b is greater”, either one of the strings is returned, but never both.

Its the same as saying:

if (a > b) { return 'a is greater' } else { return 'b is greater' }


Much thanks to you. Clear now!!

1 Like