This is a destructuring assignment is not an array declaration. This code:
const [, targetItems , remainItems] = matches
Is equivalent to:
const targetItems = matches[1]
const remainItems = matches[2]
You're basically create two variables binding them to the value of the second and third element in the array. The comma is used so you can skip the element you're not interested. You can also use the spread syntax, so for example:
const [, x, , y, ...end] = ['a','b','c','d','e', 'f']
console.log(x) // "b"
console.log(y) // "d"
console.log(end) // ["e", "f"]
Said that, you can definitely also create arrays with empty slots:
const arr = [, 10];
In this case, matches should be[a, b, c], but what the final array should look like?
In the code you posted there is no return
for the array function (that use brackets so the statement is needed) therefore the array would be mapped to undefined
for each element.
Update
const matches = itmes.match([a-z])
Assuming itmes
is a typo for items
, I would also assume that items.match([a-z])
is actually items.match(/[a-z]/)
since it wouldn't make sense otherwise – match takes a regexp as argument, even with type coercion it would make less sense the code comparing to a regexp typo.
However, even in that case the code won't work, since match
would returns just one element, makes the whole destructuring useless – the only element returned is the one discarded. Plus, it would returns only one letter.
I believe the original code would be slightly different (e.g. having a global flag for the regexp, and / or some quantifiers, such "+"); so it's hard to figure out what would be the end results from the code provided.