Why do I encounter an error trying to use the ternary operator within this function?

I thought I understood how to use the ternary operator but now I’m unsure as I encounter an error with the following code:

const plantNeedsWater = function(day) {
  day === 'Wednesday' ? return true : return false;
};

console.log(plantNeedsWater('Wednesday'));

I did figure out a way to make the following code work but I don’t know why it works:

const plantNeedsWater = function(day) {
  return day === 'Wednesday' ?  true : false;
};

console.log(plantNeedsWater('Wednesday'));

Hello Howiek !

Two or three things to consider

In a function a return word, return the value you asked for, and the rest of your code inside the function will not be read
Secondly in a ternary consider that you return the answer of a question, your condition is true or false ? If its true the answer is ( first parameter after ? ) if it’s wrong ( second parameter )

And for your case you cans simplify your line, if you try return day === 'Wednesday you will get true or false ( a comparaison returns true or false you don’t have to precise it )

sorry if I made some languauge mistakes, je suis français ahah

2 Likes