logo
post image

Javascript Logical Nullish Assignment Operator

The logical nullish assignment operator represented by ??= works like the assignment operator =, but assigns only when the left-hand side is a nullish value.

// set x to y only when x is nullish
x ||= y;

What is a nullish value ? A nullish value in Javascript is either null or undefined.

Examples

// a is nullish as it has undefined value
let a;
let b = 10;

a ??= b;

// 10
console.log(a);
// a is not nullish
let a = 5;
let b = 10;

a ??= b;

// 5
console.log(5);
// a is nullish as it has null value
let a = null;
let b = 10;

a ??= b;

// 10
console.log(10);

Browser Compatibility

Logical OR assignment operator is supported in all major browsers :

  • Chrome 85+ (to be released in August 2020)
  • Firefox 79+
  • Edge 85+ (to be released in August 2020)
  • Safari 14+