?? in JavaScript: the nullish coalescing operator.
Samuel Kendrick

Samuel Kendrick @adnauseum

Joined:
Jun 4, 2018

?? in JavaScript: the nullish coalescing operator.

Publish Date: May 2 '23
0 0
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

Comments 0 total

    Add comment