Javascript ?? vs ||
Siddharth Arora
1 min read
Found this great article → https://www.trevorlasn.com/blog/javascript-logical-operators
This is a small and useful snippet that makes it very clear on when to use ?? and when to use ||
// Let's see what || considers "falsy":
0 || "default" // returns "default"
"" || "default" // returns "default"
false || "default" // returns "default"
null || "default" // returns "default"
undefined || "default" // returns "default"
// Now compare with ??:
0 ?? "default" // returns 0 ✨
"" ?? "default" // returns "" ✨
false ?? "default" // returns false ✨
null ?? "default" // returns "default"
undefined ?? "default" // returns "default"
0
Subscribe to my newsletter
Read articles from Siddharth Arora directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by