Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 26, 2020 01:24 pm GMT

How to use the nullish coalescing operator (??) in Javascript

The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.

Simply put, the nullish coalescing operator let's us truly check nullish values instead of falsey values. In JavaScript, a lot of values are falsey, like the number zero, an empty string, undefined, null, false, NaN etc.

Check the code below:

console.log(NaN ? "truthy" : "falsy"); // falsyconsole.log("" == false); //trueconsole.log(0 == false); //trueconsole.log(null ? "truthy" : "falsy"); //falsyconsole.log(undefined ? "truthy" : "falsy"); //falsyconsole.log(false == false); //true// NaN, undefined and null are not really falseconsole.log(NaN == false); //falseconsole.log(undefined == false); // falseconsole.log(null == false); //false

NaN, undefined and null are not really equal to false but they are falsey, that's why I didn't make a check against false and I used a condition instead.

With this in mind, let's now look at some examples in which we'll first use the OR operator and then the nullish operator:

// using the logical OR (||) operatorconst or_printSomething_1 = null || "random string";console.log(or_printSomething_1); // prints "random string"const or_printSomething_2 = undefined || "another random string";console.log(or_printSomething_2); // prints "another random string"const or_printSomething_3 = 0 || "what? another string?";console.log(or_printSomething_3); // prints "what? another string?"const or_printSomething_4 = false || "ok? too many strings!";console.log(or_printSomething_4); // prints "ok? too many strings!"// using the logical nullish (??) operatorconst nullish_printSomething_1 = null ?? "random string";console.log(nullish_printSomething_1); // prints "random string"const nullish_printSomething_2 = undefined ?? "another random string";console.log(nullish_printSomething_2); // prints "another random string"const nullish_printSomething_3 = 0 ?? "what? another string?";console.log(nullish_printSomething_3); // prints 0const nullish_printSomething_4 = false ?? "ok? too many strings!";console.log(nullish_printSomething_4); // prints false

As you can see, when using ||, the left side is always evaluated to false and the right side gets printed to the console. When using ?? and the left side is equal to undefined or null, only then the right side is returned.

Being a feature of ES2020, the nullish coalescing operator is not fully supported by all browsers, so you can check for compatibility here.

Image source: Startup Stock Photos/ @startup-stock-photos on Pexels


Original Link: https://dev.to/cilvako/how-to-use-the-nullish-coalescing-operator-in-javascript-1c0p

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To