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)