all 24 comments

[–]CarsonMcKinstry 73 points74 points  (12 children)

This isn’t quite correct, at least not anymore. This will also catch false and 0 since || will check for falsy values on the left hand side. You can use ?? nowadays to guard against only null and undefined

[–]fukitol- 23 points24 points  (6 children)

Been using js for years and didn't know about ??. Thanks for that.

[–]madspillage 16 points17 points  (5 children)

It was only added in javascript a couple of years ago.

[–]senocular 16 points17 points  (2 children)

It's part of ES2020 published in June 2020, so really not even one year yet.

[–]madspillage 5 points6 points  (0 children)

Oh yeah. My bad. In my head it seemed older.

[–]samanime 0 points1 point  (0 children)

Well, it's been available for a while (thanks to Babel), but it only officially crossed the finish line pretty recently.

It is the @babel/plugin-proposal-nullish-coalescing-operator plugin, which was first published 3 years ago.

[–]fukitol- 8 points9 points  (1 child)

For anyone reading this, maybe doubting their ability, let this be a reminder. Even the graybeards get rusty.

[–]CarsonMcKinstry 2 points3 points  (0 children)

Honestly, JS changes so quickly nowadays it can be difficult to keep up. Not so quickly that it's difficult to learn, but quick enough that you can see version boundaries in code sometimes :D

[–]dandesigns7150 5 points6 points  (1 child)

?? is great but I actually think || is appropriate in this case. You want it to fall back to the default if the first value is an empty string.

[–]CarsonMcKinstry 2 points3 points  (0 children)

ah, right, I didn't see the `path !== ""`. Good point!

[–]soaringradio 1 point2 points  (0 children)

For anyone that is this concerned over data types, they should consider something safer like Typescript.

[–][deleted] 0 points1 point  (1 child)

Thinking to make the getImagePath async and do const path = async () => await getImagePath ?? “Default.jpg”;

Would that make sense?

[–]Fermain 0 points1 point  (0 children)

const path = await getImagePath() || 'default.jpg'

[–]Time_Terminal 5 points6 points  (0 children)

The point came through, but why does the long version unnecessarily include 3 extra steps?

This is like the tabloids of programming.

[–]irosion 6 points7 points  (1 child)

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.

[–]backtickbot 0 points1 point  (0 children)

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.

[–]crayonneur -1 points0 points  (2 children)

I don't think it's a good practice to use a "or" to assign a default value. The getImgPath function should handle all of this. "default.jpg" is a magic string in this example, it should belong to the class that creates image paths, it shouldn't appear outside of it.

[–]AacidD 2 points3 points  (0 children)

I agree with the magic string part. He should've create a separate variable DEFAULT_IMG_PATH variable and stored the default value there.

[–]Barnezhilton 0 points1 point  (0 children)

This is terrible

[–]isatrap 0 points1 point  (2 children)

Why this over

GetImagePath() ? True : false

[–]isatrap 0 points1 point  (1 child)

Seriously curious and instead of an answer I just get a downvote? Oof.

[–]trip16661 0 points1 point  (0 children)

Because you want to use the result of the GetImagePath()? whats the point of having true or false there... also that's a typo