-1

I am not sure what this type of array with no name is called. When I try to google "javascript array starts with comma" I get unrelated results. I am not sure what is going on in the code. In the You Don't Know Javascript Yet book there is no explanation.

var [ , meetingStartHour, meetingStartMinutes ] =
    startTime.match(/^(\d{1,2}):(\d{2})$/) || [];

Simpson, Kyle. You Don't Know JS Yet: Get Started (p. 118). GetiPub & Leanpub. Kindle Edition.

smuggledPancakes
  • 8,687
  • 18
  • 64
  • 104
  • 1
    Does `[, two, three] = [1, 2, 3]` clarify? It's part of [array destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Ignoring_some_returned_values). – jonrsharpe May 12 '20 at 14:48
  • Does this answer your question? [Multiple assignment in javascript? What does \[a,b,c\] = \[1, 2, 3\]; mean?](https://stackoverflow.com/questions/3986348/multiple-assignment-in-javascript-what-does-a-b-c-1-2-3-mean) – Ivar May 12 '20 at 14:52

2 Answers2

2

This is called Destructuring.

From the linked page:

function f() {
  return [1, 2, 3];
}

const [a, , b] = f();
console.log(a); // 1
console.log(b); // 3
tdurtsch
  • 131
  • 1
  • 4
2

This is basically a destructuring syntax,

var arr = [1,2,3];

var [,a,b] = arr;

console.log("Second element of arr is ", a);
console.log("Third element of arr is ", b);

to get second and third element from an array and leaving the first one.

meetingStartHour : gets the second element from startTime.match(/^(\d{1,2}):(\d{2})$/) || [];

meetingStartMinutes : gets the third element from startTime.match(/^(\d{1,2}):(\d{2})$/) || [];

startTime.match(/^(\d{1,2}):(\d{2})$/) || []; must be returning a array of 3 elements

adroit18
  • 313
  • 1
  • 3