
What is the Nullish Coalescing Operator?
??
).??
operator returns the second operand if the first operand is null
or undefined
. If the first operand is not null
or undefined
, it returns the first operand.??
do if Ali either went to school or didn't?// Initially, Ali didn't do anything // meaning the value of 'a' is by default null or undefined. let a = undefined; // or null; let b = "did not go"; let result = a ?? b; // The result is 'b' because Ali didn't go, as the value is undefined. // When does the 'a' variable work? 🤔 // The value of 'a' works when Ali went to school, meaning it's not null or undefined. // We write any value like "went" to the 'a' variable. a = "went"; let result = a ?? b; // The result is 'a' because Ali went.
Syntax
let result = a ?? b;
- If
a
isnull
orundefined
, the variableresult
will be assigned the value ofb
.
- Otherwise,
result
will be assigned the value ofa
.
Example:
let name = null; let defaultName = "Guest"; let displayName = name ?? defaultName; console.log(displayName); // "Guest"
name
variable is null
, the displayName
variable takes the value of defaultName
, which is "Guest"
.Nullish Coalescing Operator vs. Logical OR (||
)
||
(Logical OR) operator can be confused with the ??
operator. However, their working principles are different:- The
||
operator treats values likefalse
,0
, and an empty string""
as false and returns the second operand.
- The
??
operator only considersnull
andundefined
.
Example:
let count = 0; let result1 = count || 10; console.log(result1); // 10, because 0 is a falsy value. let result2 = count ?? 10; console.log(result2); // 0, because 0 is not null or undefined.
count
is equal to 0
. The ||
operator treats 0
as false and returns 10
. However, the ??
operator accepts the value 0
because it is not null
or undefined
.Conclusion
??
) is more convenient when working with default values, specifically when dealing with null
or undefined
. This operator is particularly useful when you don't want to replace values like false
, 0
, or an empty string with default values.Similar Articles
Main features of Reactjs
In this article, we will learn about the key features of React.js, which will also be useful for interview questions.
August 21, 2024React Native: Why Choose React Native for Mobile Development?
The field of mobile development is rapidly evolving, and developers constantly seek efficient ways to create applications for both iOS and Android platforms simultaneously. React Native has emerged a
December 15, 2024Destructuring assignment in JavaScript
In today's article, we will explore in detail another important JavaScript syntax: Destructuring assignment.
August 17, 2024First Class Function in JavaScript
Hello friends. In today's post, we'll go into detail about another important concept in JavaScript that plays a crucial role First Class Functions!
August 14, 2024What is React js? What role does React play in software development?
Today, we'll discuss React.js, one of the top JavaScript libraries, including its advantages, what we can do with it, and everything else you need to know in this post.
August 20, 2024What is an SPA (Single Page Application)?
Let's explore what an SPA (Single Page Application) is, which is used in today's modern websites.
August 29, 2024