Nested Destructuring in JavaScript

Level 3143 keystrokes

Nested destructuring extracts values from nested objects in a single statement.

JavaScript code

// Create an object with nested address object
1const user = {
2 name: "Alice",
3 address: { city: "Tokyo", zip: "100-0001" }
4};
// Destructure both the name and the nested city
// address: { city } means "from address, get city"
5const { name, address: { city } } = user;
// Print the extracted values
6console.log(name, city);

Output

Alice Tokyo
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.

  • "× 6
  • :× 5
  • ,× 4
  • {× 4
  • }× 4
  • ;× 3
  • =× 2
  • -× 1
  • .× 1
  • (× 1

More JavaScript examples at level 3

← JavaScript typing practice