tc39/proposal-nullish-coalescing

Name: proposal-nullish-coalescing

Owner: Ecma TC39

Description: Nullish coalescing proposal x ?? y

Created: 2017-07-20 16:05:24.0

Updated: 2018-05-24 09:54:28.0

Pushed: 2018-02-28 04:58:44.0

Homepage: https://tc39.github.io/proposal-nullish-coalescing/

Size: 24

Language: HTML

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Nullish Coalescing for JavaScript

Status

Current Stage:

Authors
Overview and motivation

When performing optional property access in a nested structure in conjunction with the optional chaining operator, 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.

t response = {
ttings: {
nullValue: null,
height: 400,
animationDuration: 0,
headerText: '',
showSplashScreen: false



t undefinedValue = response.settings?.undefinedValue || 'some other default'; // result: 'some other default'
t nullValue = response.settings?.nullValue || 'some other default'; // result: 'some other default'

This works well for the common case of null and undefined values, but there are a number of falsy values that might produce surprising results:

t headerText = response.settings?.headerText || 'Hello, world!'; // Potentially unintended. '' evaluates to false, result: 'Hello, world!'
t animationDuration = response.settings?.animationDuration || 300; // Potentially unintended. 0 evaluates to false, result: 300
t showSplashScreen = response.settings?.showSplashScreen || true; // Potentially unintended. False evaluates to false, result: true

The nullary coalescing operator is intended to handle these cases better and serves as an equality check against nullary values (null or undefined).

Syntax

Base case. If the expression at the left-hand side of the ?? operator evaluates to undefined or null, its right-hand side is returned.

t response = {
ttings: {
nullValue: null,
height: 400,
animationDuration: 0,
headerText: '',
showSplashScreen: false



t undefinedValue = response.settings?.undefinedValue ?? 'some other default'; // result: 'some other default'
t nullValue = response.settings?.nullValue ?? 'some other default'; // result: 'some other default'
t headerText = response.settings?.headerText ?? 'Hello, world!'; // result: ''
t animationDuration = response.settings?.animationDuration ?? 300; // result: 0
t showSplashScreen = response.settings?.showSplashScreen ?? true; // result: false
Notes

While this proposal specifically calls out null and undefined values, the intent is to provide a complementary operator to the optional chaining operator. This proposal will update to match the sementics of that operator.

Prior Art
Specification
TODO

Per the TC39 process document, here is a high level list of work that needs to happen across the various proposal stages.

References
Prior discussion

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.