Nullish Coalescing Operator

const value = obj.value || 'default value';
console.log(null || 'default value');      // default value
console.log(undefined || 'default value'); // default value
console.log(false || 'default value'); // default value
console.log(0 || 'default value'); // default value
console.log('' || 'default value'); // default value
console.log(NaN || 'default value'); // default value
console.log(null ?? 'default value');      // default value
console.log(undefined ?? 'default value'); // default value
console.log(false ?? 'default value'); // false
console.log(0 ?? 'default value'); // 0
console.log('' ?? 'default value'); // ''
console.log(NaN ?? 'default value'); // NaN
const value = nested?.first ?? 'no value';
$ yarn add @babel/plugin-proposal-nullish-coalescing-operator -D
{
"plugins": [
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-proposal-nullish-coalescing-operator"
]
}

--

--

--

Previously Paris and Geneva, currently New York. Can also be found at https://dev.to/arnaud

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Arnaud Dostes

Arnaud Dostes

Previously Paris and Geneva, currently New York. Can also be found at https://dev.to/arnaud

More from Medium

Learn TypeScript With Examples

Angular with Jest Testing

Polling simplified, with React Query (useQuery)

Server-Side Events as Simple Alternative to WebSockets