Search code examples
javascriptarraysdestructuring

How to use array destructuring on Javascript array with index of not 0?


I have the following variables:

$firstSection = $main.children[0];
$secondSection = $main.children[1];

To use array destructuring on the first variable, I can do this: [$firstSection] = $main.children;

However, how am I supposed to use array destructuring on the second variable? Thanks.


Solution

  • Just put the second item to destructure to the right of the first, in a comma-separated list. It looks very similar to when declaring an array with 2 items.

    const [$firstSection, $secondSection] = $main.children;