Trying to understand terniary operator

this if/else statement passes tests

if (typeof bool === ‘boolean’) {
return true;
} else {
return false;

But this one always returns undefined.

typeof bool === ‘boolean’ ? true : false;

Can anyone tell me why this is?

Your second one doesn’t have a return statement.

You don’t have a return statement with the ternary Operator, try with:

return typeof bool === ‘boolean’ ? true : false;

Thanks, it seems so obvious once someone points it out.

Just as a side note,. if you’re doing comparisons & you want true/false then you don’t need the ? true : false


return typeof bool === 'boolean';

will do.

1 Like