nullish operator javascript code example

Example 1: nullish coalesing

Expression:
	Left ?? Right
if left is null or undefined , then Right will be the value 

const a = '' ;
const b = undefined;

const c = a ?? 'default' ; // will give c =''
const d = b ?? 'default' ; // wil give d = 'default'

Example 2: logical nullish operator javascript

function config(options) {
  options.duration ??= 100;
  options.speed ??= 25;
  return options;
}

config({ duration: 125 }); // { duration: 125, speed: 25 }
config({}); // { duration: 100, speed: 25 }

Tags:

Misc Example