Sitemap
Aarif Mahdi

Aarif Mahdi

nullish coalescing: An important concept in JavaScript

Dec 14, 2025
Press enter or click to view image in full size

Lets understand nullish coalescing with an example:

  • We wanted a whole number
  • We used 0 and 2, both are whole numbers
  • But OR operator “||” returned 2
  • Why? because it uses the truthy/falsy concept
  • || operator returns the first truthy value
  • 0 is a falsy value so it is neglected!

Get Aarif Mahdi’s stories in your inbox

Join Medium for free to get updates from this writer.

Solution:

  • we used “??” nullish coalescing operator
  • syntax: leftExpression ?? rightExpression
  • How it works?
    if leftExpression is null or undefined it returns rightExpression (expression on the right side, duh)
    if first expression (leftExpression OR expression on the left side, duh) is not null or undefined it returns it (even if its some other falsy value like 0, empty string “”, etc)
  • nullish coalescing operator only checks null/undefined
  • it does not check truthy/falsy values

--

--