Introduction
Default value plays very important role when we are implementing some functionality in javascript, When function called with some missing values then if function definition is not provided with any default value then we will not get the expected return value from function. For example,
function sampleFunc(a,b){
console.log(a+b);// a gets value 2 and b gets value undefined
}
sampleFunc(2);
If we execute above code we will get NaN
as output.
How we can assign default value if value is missing?
Before introducing Nullish Coalescing default value assignment done using ||
operator. This operator assigns value which is not in the falsy value list. So Except falsy values it assigns any given values to the variable.
function sampleFunc(a,b){
var a= a||2;//If a has falsy value then takes 2 as value of a
var b= b||3//If b has falsy value then takes 3 as value of b
console.log(a+b);
}
sampleFunc(2);
In the above code example if any value is missing while passing then it takes 2
as default value for a
and 3
as default value for b
. So we get some output without any error.
What is Nullish Coalescing Operator?
Nullish Coalescing is an operator which takes 2 operands left hand value
and right_hand_value
if the left_hand_value
is null
or undefined
then it returns right_hand_value
.
Syntax: left_hand_value ?? right_hand__value
function sampleFunc(){
var a;//Initialized with 'undefined'
let b = a ?? 3;//if a is 'null' or 'undefined' then assigns 3 to b
console.log(b);
}
sampleFunc()
When we should prefer ?? instead of ||?
In ||
operator, It takes right hand value when left hand value is falsy i.e even if we want to take 0
as input ||
operator doesn't allow, because 0
comes under falsy set of values it takes right hand value. So in the cases where we want any other values except null
and undefined
then ??
operator helps to get correct values for input.
How it can be used to assign default value?
When we are expecting some values for parameters in function definition but they are not passed through function call then it is better to keep some default values for all parameters in function definition so that variable can take some value and do processing without interrupting the execution process. So now we have new operator called Nullish Coalescing, This can be used to assign some default values to variables when parameter value is null
or undefined
. For example,
function sampleFunc(a,b){
let m = a ?? 2;//If a is null or undefined then m gets value 2
let n = b ?? 3;//If b is null or undefined then n gets value 3
console.log(m+n);
}
sampleFunc(4);
In the above code only one value passed, Since value gets assigned from left hand side of the parameter list a
gets value 4
and b is initially assigned with value undefined
then using Null Coalescing operator since b
is undefined
variable n
is assigned with value of 3
. This is how Nullish Coalescing can be used to assign default values to variables.
Note : This operator assigns
right_hand_value
to variable if and only if theleft_hand_value
isnull
orundefined
, Other than these 2 values if any falsy value present onleft_hand_side
then assigns that value itself.
Thank you