DEV Community

Naman Kumar
Naman Kumar

Posted on

JavaScript || Logical OR (||) vs Nullish Coalescing Operator (??) in JavaScript

With the latest release of version 14, the Nullish Coalescing Operator (??) is now supported in NodeJS. In this post let us see what is the use case of this operator and how is it different from the logical OR.

Logical Or (||)

const paginate = (options = {}) => {
  return [1, 2, 3, 4, 5].splice(0, options.limit || 3);
}

paginate(1); // expected: [1], output: [1]
paginate(); // expected: [1, 2, 3], output: [1, 2, 3]
paginate(0); // expected: [], output: [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

How the logical or operator functions is, it returns the right hand value if the left hand value coerce to false. And that not only includes undefined and null but also 0 and ''.

In many of our use cases, like the one above this causes unexpected results and we end up using the typeof operator.

Nullish Coalescing Operator (??)

This solves the problem for us. This operators returns the right hand value only if the left hand value is either null or undefined.

const paginate = (options = {}) => {
  return [1, 2, 3, 4, 5].splice(0, options.limit ?? 3);
}

paginate(1); // expected: [1], output: [1]
paginate(); // expected: [1, 2, 3], output: [1, 2, 3]
paginate(0); // expected: [], output: []
Enter fullscreen mode Exit fullscreen mode

Share your quick JavaScript bites in comments.

Top comments (4)

Collapse
 
hrdyjan1 profile image
hrdyjan1

There should be probably "limit" only, no "options/options.limit".

Collapse
 
palcisto profile image
Patrick Alcisto

Nice article but, @ hrdyjan1 is right, your example implies it expects an object with a possible limit property, yet you never pass the limit as part of an object in the three invocations.

Collapse
 
tonydev_ profile image
TonyDev ๐Ÿ‘พ • Edited

options.limit is always undefined
All cases return [1, 2, 3]

Collapse
 
aniruddhadas1 profile image
Aniruddha Das

Thanks, clears the basic concept.