Destructure array and use object shorthand assignment I have the following code:

let id = '';
let text = '';

// Note: `match` = [full match, id (first group), text (second group)]
if (match.length &gt; 1) {
  [, id] = match;
}
if (match.length &gt; 2) {
  [, id, text] = match;
}
return { id, text };


match could be null, or lengths between 1-3.

id and text need to be empty strings by default and only set if match is not null and of certain length.

How can I shorten this up?

Possibly avoiding the if statements.
I discovered that array destructing has the capability of providing default values.

const [, id = '', text = ''] = match;
return { id, text };


Array Destructuring