Array Destructuring in JavaScript

Level 277 keystrokes

Destructuring allows extracting values from arrays into separate variables.

JavaScript code

// Create an array with three numbers
1let nums = [1, 2, 3];
// Destructure the array into variables
// first gets 1 (index 0), second gets 2 (index 1); 3 is not used
2let [first, second] = nums;
// Print both values with a space between
3console.log(first, second);

Output

1 2
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.

  • ,× 4
  • ;× 3
  • [× 2
  • ]× 2
  • =× 2
  • .× 1
  • (× 1
  • )× 1

More JavaScript examples at level 2

← JavaScript typing practice