JavaScript `??=` Operator: Simplifying Nullish Assignments

2 min read .

The ??= operator in JavaScript is a logical assignment operator designed to simplify the process of assigning values when dealing with null or undefined. This operator, introduced in ECMAScript 2021 (ES2021), streamlines conditional assignments by combining the nullish coalescing operator (??) with the assignment operator (=).

What is the ??= Operator?

The ??= operator is a shorthand that combines the nullish coalescing operator with assignment. It updates the left-hand side operand only if it is null or undefined, assigning it the right-hand side value. If the left-hand side operand is neither null nor undefined, no assignment takes place.

Syntax:

variable ??= value;
  • variable: The variable to be conditionally updated.
  • value: The value to assign to variable if variable is null or undefined.

Why Use the ??= Operator?

  • Conciseness: Reduces the amount of code needed to set default values for null or undefined variables.
  • Clarity: Makes it clear that default values are only applied when the variable is specifically null or undefined, avoiding unintended assignments.
  • Efficiency: Simplifies common patterns of conditional assignments, enhancing code readability and maintainability.

How to Use the ??= Operator

  1. Basic Usage

The ??= operator assigns a default value only when the variable is null or undefined:

let userName = null;
let defaultName = 'Guest';

// Assign defaultName if userName is null or undefined
userName ??= defaultName;

console.log(userName); // Output: Guest
  1. Setting Defaults for Function Parameters

Use ??= to provide default values for function parameters that may be null or undefined:

function greet(name) {
  name ??= 'Stranger'; // Default to 'Stranger' if name is null or undefined
  console.log(`Hello, ${name}!`);
}

greet('Alice'); // Output: Hello, Alice!
greet(); // Output: Hello, Stranger!
  1. Updating Object Properties

Assign default values to properties within objects:

let settings = {
  theme: undefined,
  fontSize: 16
};

// Update theme only if it's null or undefined
settings.theme ??= 'Light';

console.log(settings.theme); // Output: Light
  1. Handling Arrays

Use ??= to assign default values to arrays:

let items = null;
let defaultItems = [1, 2, 3];

// Assign defaultItems if items is null or undefined
items ??= defaultItems;

console.log(items); // Output: [1, 2, 3]
  1. Combining with Other Operators

Combine ??= with other logical or assignment operators for more complex scenarios:

let count = null;
let incrementValue = 5;

// Update count if it's null or undefined
count ??= incrementValue;

console.log(count); // Output: 5
  1. Default Values with Nested Properties

Assign default values to nested properties within objects:

let userConfig = {
  theme: null,
  language: 'en'
};

// Assign default theme if it is null or undefined
userConfig.theme ??= 'Dark';

console.log(userConfig.theme); // Output: Dark

Comparison with Other Operators

  • ?? (Nullish Coalescing): The ?? operator returns the right-hand side operand if the left-hand side is null or undefined.

    let value = null;
    value ??= 'default'; // value is assigned 'default' because null is nullish
    
  • || (Logical OR): The || operator assigns the right-hand side operand if the left-hand side is falsy (including 0, false, NaN, '', etc.), which can lead to unintended defaults if falsy values are valid.

    let value = 0;
    value ||= 'default'; // value is assigned 'default' because 0 is falsy
    

Browser Compatibility

The ??= operator is supported in modern JavaScript environments including recent versions of Chrome, Firefox, Safari, and Edge. It is compatible with ES2021 and later. Ensure compatibility with older browsers if necessary.

Conclusion

The JavaScript ??= operator simplifies conditional assignments by combining nullish coalescing with assignment, providing a clean and efficient way to handle null and undefined values. By reducing code verbosity and enhancing clarity, ??= is a valuable tool for modern JavaScript development.

See Also

chevron-up