Logo

dev-resources.site

for different kinds of informations.

Nullish Coalescing Operator

Published at
12/2/2022
Categories
javascript
typescript
operators
Author
blamb31
Categories
3 categories in total
javascript
open
typescript
open
operators
open
Author
7 person written this
blamb31
open
Nullish Coalescing Operator

Overview & Setup

This article will show the usage of the Nullish Coalescing Operator in Javascript. I will also discuss the benefit of using this operator in your code.

The demo I will do is a node.js project, but this can be done in any Javascript file. You can create a new Stackblitz Node project, or use mine here.

Getting Into It

The Nullish Coalescing Operator is a JavaScript operator that was introduced recently. It is similar to the OR (||) operator, but wil some cool difference. With the traditional OR operator, a condition would evaluate to false if the value was falsy. This meant that it would be evaluate to false on null, undefined, 0, '', NaN and false. With the Nullish Coalescing Operator (??), it will only evaluate to false if the value is null or undefined. This could present some problems and potential bugs in your code. For example, you may want to show a number as long as there is a number to show, even if the number is 0. So you could write a block of code like this:

//index.js

team.score = 0;
const score = team.score ?? "No Score Added";
console.log(score); // 0
Enter fullscreen mode Exit fullscreen mode

This would assign the score to be any value held in team.score other than null or undefined. In this case score would be assigned the value of 0.

Instead if you wrote:

//index.js

team.score = 0;
const score = team.score || "No Score Added";
console.log(score); // 'No Score Added'
Enter fullscreen mode Exit fullscreen mode

the score variable would be 'No Score Added' because team.score is 0 which is a falsy value. In a case like this, we obviously would not want that to be the result, so the ?? is the perfect replacement operator.

Wrap Up

The Nullish Coalescing Operator is a cool new operator that can clean up your code and help out a lot. Only evaluating to false on null and undefined can be extremely useful. But at the same time it can create some problems if you don't understand exactly what it is doing. Hopefully after reading this you are more clear on the use of it and can add it to your arsenal of JavaScript Operator.

For some examples of the difference between the || and ?? operators, or to try it out yourself, you can visit this Stackblitz (Or take a look at the code below).

//index.js

console.log(`What will I Show?? ${null || hi}`);
// result: hi
console.log(`What will I Show?? ${undefined || hi}`);
// result: hi
console.log(`What will I Show?? ${NaN || hi}`);
// result: hi
console.log(`What will I Show?? ${false || hi}`);
// result: hi
console.log(`What will I Show?? ${0 || hi}`);
// result: hi
console.log(`What will I Show?? ${"" || hi}\n\n`);
// result: hi

console.log(`What will I Show?? ${null ?? hi}`);
// result: hi
console.log(`What will I Show?? ${undefined ?? hi}`);
// result: hi
console.log(`What will I Show?? ${NaN ?? hi}`);
// result: NaN
console.log(`What will I Show?? ${false ?? hi}`);
// result: false
console.log(`What will I Show?? ${0 ?? hi}`);
// result: 0
console.log(`What will I Show?? ${"" ?? hi}`);
// result: ''
Enter fullscreen mode Exit fullscreen mode
operators Article's
30 articles in total
Favicon
Essential MySQL Operators and Their Applications
Favicon
Exposing replica nodes in Percona Operator for PostgreSQL
Favicon
It’s just β€˜,’ – The Comma Operator
Favicon
Operators, Conditionals and Inputs
Favicon
Practical Guide to Python Conditional Statements
Favicon
Python Operators Demystified
Favicon
SQL Operators Made Easy for Beginners
Favicon
First Steps in SQL Operators: A Beginner's Guide
Favicon
AND / OR operators, Short-Circuiting and Nullish Coalescing in Javascript
Favicon
From Zero to Hero: Disaster Recovery for PostgreSQL with Streaming Replication in Kubernetes
Favicon
Google Search Operators & Usage Tips
Favicon
Operators in C programming
Favicon
MySQL Operators – A Guide
Favicon
Annotations in Kubernetes Operator Design
Favicon
Exploring the unusual: JavaScript arrays and the 'in' operator
Favicon
Install Kubernetes Controllers via Operators - ARGO CD
Favicon
Mastering Advanced JavaScript Operators: The Ultimate Guide
Favicon
Operators in JavaScript: The Fundamentals
Favicon
Dart as, is, is! operatΓΆrleri
Favicon
Nullish Coalescing Operator
Favicon
Difference between ? and ?? in JavaScript/Typescript
Favicon
Ordering Event Bus Events with RxJS and concatMap
Favicon
Division, Floor Division and Modulus - Python Arithmetic Operators every beginner should know.
Favicon
Operators in Python
Favicon
Angular - Rxjs - Operator mergeAll
Favicon
Angular - Rxjs - Operator map
Favicon
Swift β€” 11 Useful Combine Operators You Need to Know
Favicon
Cloud Native CICD Pipelines in OpenShift
Favicon
Kubernetes Operators to realize the dream of Zero-Touch Ops
Favicon
JavaScript Basic - Variable, Data Types, Operators, Comparisons

Featured ones: