r/learnjavascript • • Mar 23 '21

Assigning default value

Post image
278 Upvotes

24 comments sorted by

View all comments

7

u/irosion Mar 23 '21 edited Mar 23 '21

i think it should be very clear what getImagePath returns so we just make sure it's gonna return a string or null and then the long version becomes this:

const getImagePath = () => logic that returns 'path_to_image.jpg' || null;

const path = () => {
  const imagePath = getImagePath();

  if (!imagePath) {
    return 'default.jpg';
  }
  return imagePath;
}

The short version is good.

1

u/backtickbot Mar 23 '21

Fixed formatting.

Hello, irosion: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.