DEV Community

Vikas Raj
Vikas Raj

Posted on

ECMASCRIPT: Nullish Coalescing

Nullish Coalescing is at Stage 3. This one is interesting.

GitHub logo tc39 / proposal-nullish-coalescing

Nullish coalescing proposal x ?? y

Nullish Coalescing for JavaScript

Status

Current Stage:

  • Stage 4

Authors

Overview and motivation

When performing property accesses, it is often desired to provide a default value if the result of that property access is null or undefined. At present, a typical way to express this intent in JavaScript is by using the || operator.

const response = {
  settings: {
    nullValue: null,
    height: 400,
    animationDuration: 0,
    headerText: '',
    showSplashScreen: false
  }
};
const undefinedValue = response.settings.undefinedValue || 'some other default'; // result: 'some other default'
const nullValue = response.settings.nullValue || 'some other default'; // result: 'some other default'

This works well for the common case of null

Top comments (0)