r/learnjavascript • • Mar 23 '21

Assigning default value

Post image
278 Upvotes

24 comments sorted by

View all comments

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

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'