Javascript if/else shorthand

I tried to turn this (working) code:

const plantNeedsWater = function(day) {
if (day === ‘Wednesday’) {
return true;
} else {
return false;
}
};
console.log(plantNeedsWater(‘Wednesday’));

Into this in order to reduce what is needed

const plantNeedsWater = function(day) {
day === ‘Wednesday’ ? true: false;
}
console.log(plantNeedsWater(‘Wednesday’));

However, when using the second code I get ‘undefined’ printed rather than ‘true’. What is this happening and is there a way to get the second code to behave like the first one?

Edit: Found solution in a example a few exercises later (after being too stubborn to move on at first)

return day === ‘Wednesday’ ? true: false;

fixes the problem

2 Likes

A comparison results in Boolean value, so you could even do:

return day === 'Wednesday'