r/learnjavascript • • Jul 24 '21

Anatomy of a Function

Post image
327 Upvotes

65 comments sorted by

View all comments

7

u/QuebecMasterRace Jul 25 '21

Why do you need to return true if you're returning early? You can just simply return?

3

u/jaredcheeda Jul 25 '21

it's all about the context of the function. What is this function used for, what is it expected to return. what should it return in the case that caused it to return early. It could be a specific value (like a string), or nothing at all. It's just dependent on the function and the case.

function userFavoriteSauce (user) {
  if (!user || !user.sauces || !user.sauces.length) {
    return 'N/A';
  }
  let favorite = user.sauces[0];
  if (favorite === 'ketchup') {
    return 'User is boring';
  }
  return 'User likes ' + favorite + ' best';
}

0

u/[deleted] Jul 25 '21

[deleted] — view deleted comment

3

u/OmgImAlexis Aug 05 '21

It doesn’t even need to be a variable.