VOOZH about

URL: https://www.geeksforgeeks.org/javascript/javascript-nullish-coalescing-operator/

⇱ JavaScript Nullish Coalescing(??) Operator - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

JavaScript Nullish Coalescing(??) Operator

Last Updated : 22 Jan, 2026

The nullish coalescing (??) operator is used to handle null and undefined values in JavaScript. It allows you to assign a default value when a variable does not have a valid value.

  • It returns the right-hand value only when the left-hand value is null or undefined.
  • It does not treat 0, false, or empty strings as nullish values.
  • It is useful for setting safe default values without overwriting valid data.

Example 1: In this example, we will see a basic function using the nullish coalescing operator

Syntax:

variable ?? default_value

Example 2: The more common use case is to set default values for JSON objects as follows. 

Best Practices

  • Use ?? when you only want to handle null or undefined.
  • Avoid mixing ?? with || in the same expression without parentheses.
  • Prefer ?? for default values in configuration and API responses.
Comment
Article Tags: