r/learnjavascript • • Mar 23 '21

Assigning default value

Post image
278 Upvotes

24 comments sorted by

74

u/CarsonMcKinstry Mar 23 '21

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

25

u/fukitol- Mar 23 '21

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

18

u/madspillage Mar 23 '21

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

17

u/senocular Mar 23 '21

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

6

u/madspillage Mar 23 '21

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

1

u/samanime Mar 23 '21

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.

9

u/fukitol- Mar 23 '21

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

3

u/CarsonMcKinstry Mar 23 '21

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

6

u/dandesigns7150 Mar 23 '21

?? 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.

3

u/CarsonMcKinstry Mar 23 '21

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

2

u/soaringradio Mar 23 '21

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

1

u/[deleted] Mar 23 '21

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

Would that make sense?

1

u/Fermain Mar 23 '21

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

6

u/Time_Terminal Mar 23 '21

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

This is like the tabloids of programming.

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.

1

u/crayonneur Mar 23 '21

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.

3

u/AacidD Mar 23 '21

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

1

u/Barnezhilton Mar 23 '21

This is terrible

1

u/isatrap Mar 23 '21

Why this over

GetImagePath() ? True : false

1

u/isatrap Mar 23 '21

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

1

u/trip16661 Mar 23 '21

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

-8

u/mburger89 Mar 23 '21

It is generally not good practice to initialize a variable with out giving it a literal value. Cause it will initialize to null or undefined.

1

u/AacidD Mar 23 '21

How to initialize a variable without giving it a literal value?