Array Destructuring in JavaScript

Level 3130 keystrokes

Uses array destructuring with rest operator to extract elements.

JavaScript code

// Create an array of color names
1const colors = ["red", "green", "blue", "yellow"];
// Destructuring assigns by position: first = "red", second = "green"
// ...rest collects remaining elements into a new array
2const [first, second, ...rest] = colors;
// Output the first element
3console.log(first);
// Output the rest array: ["blue", "yellow"]
4console.log(rest);

Output

red
[ 'blue', 'yellow' ]
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • "× 8
  • ,× 5
  • .× 5
  • ;× 4
  • (× 2
  • )× 2
  • [× 2
  • ]× 2
  • =× 2

More JavaScript examples at level 3

← JavaScript typing practice