1

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.

4
  • Does this answer your question? Array Destructuring Skipping Values Commented Apr 19, 2021 at 23:53
  • Using $ in JavaScript variables isn't really necessary nor advised. Commented Apr 19, 2021 at 23:55
  • The reason why I'm using it here is because it's a part of the JS Styleguide that I'm currently working with. Why do you think it's not advised? @tadman Commented Apr 20, 2021 at 0:25
  • That's a very strange style guide. Commented Apr 20, 2021 at 1:07

2 Answers 2

3

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;
Sign up to request clarification or add additional context in comments.

Comments

1

The values are accessed through a comma separated list, so:

 const [$firstSection, $secondSection] = $main.children; 
 console.log($secondSection); // The second value in the $main.children array

And if you actually don't need the first value in the array, for whatever reason - you can actually just use a comma to omit the first value.

const [, $secondSection] = $main.children;
console.log($secondSection); // The second value in the $main.children array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.