The nullish-coalescing operator

In JavaScript, we can use || to set default values for variables. Besides null and undefined, this also overrides other falsy values like 0, false, and empty strings. The nullish coalescing operator ?? only overrides null and undefined.

       27 || "default"    // ⇒ 27           good
     true || "default"    // ⇒ true         good
"truthy!" || "default"    // ⇒ "truthy!"    good
     null || "default"    // ⇒ "default"    good
undefined || "default"    // ⇒ "default"    good
        0 || "default"    // ⇒ "default"    bad (overrides real value)
    false || "default"    // ⇒ "default"    bad (overrides real value)
       "" || "default"    // ⇒ "default"    bad (overrides real value)

       27 ?? "default"    // ⇒ 27           good
     true ?? "default"    // ⇒ true         good
"truthy!" ?? "default"    // ⇒ "truthy!"    good
     null ?? "default"    // ⇒ "default"    good
undefined ?? "default"    // ⇒ "default"    good
        0 ?? "default"    // ⇒ 0            good (keeps real value)
    false ?? "default"    // ⇒ false        good (keeps real value)
       "" ?? "default"    // ⇒ ""           good (keeps real value)

There is more to learn

Get free previews of my upcoming course materials and other bonus content to help you work smarter. I share tips straight to your inbox once a week. You can read previous mails in the newsletter archive.

Cannot show form because cookie consent was not given.