“Destructuring” does not mean “destructive”
It’s called “destructuring assignment”, because it “destructurizes” by copying items into variables. But the array itself is not modified.
It’s just a shorter way to write:
1 2 3 |
// let [firstName, surname] = arr; let firstName = arr[0]; let surname = arr[1]; |
Thus, we can do a shorthand like so?
1 2 3 4 5 6 7 8 |
// we have an array with the name and surname let arr = ["Ilya", "Kantor"] // destructuring assignment let [firstName, surname] = arr; alert(firstName); // Ilya alert(surname); // Kantor |
1 2 3 4 5 6 |
// first and second elements are not needed let [, , title, want] = ["Julius", "Caesar", "Consul", "of the Roman Republic"]; console.log(title); // Consul console.log(want); |