DEV Community

Samuel Kendrick
Samuel Kendrick

Posted on

?? in JavaScript: the nullish coalescing operator.

0 ?? "default";
// 0

false ?? "default";
// false

[] ?? "default"
// []

/** ⚠️ Pay attention.  */

null ?? "default";
// "default"

undefined ?? "default";
// "default"
Enter fullscreen mode Exit fullscreen mode

The ?? operator can be translated as: "return the value on the left UNLESS it's null or undefined. If the value is null or undefined, then use the value on the left of the ??.

<Avatar img={profilePicUrl ?? placeholder}/>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay