All Articles

Logical Assignment Operators

Read a fancy article from Dr. Axel Rauschmayer.

He mentioned about the logical assignment Operators ??=, &&= and ||=.

From my perspective, ??= could be very useful than the others.

Example regarding ??=

const books = [
  {
    isbn: '123',
  },
  {
    title: 'ECMAScript Language Specification',
    isbn: '456',
  },
];

// Add property .title where it’s missing
for (const book of books) {
  book.title ??= '(Untitled)';
}

assert.deepEqual(
  books,
  [
    {
      isbn: '123',
      title: '(Untitled)',
    },
    {
      title: 'ECMAScript Language Specification',
      isbn: '456',
    },
  ]);

Published Jun 15, 2020

Personal blog. I share frontend development technique and life.