Methods that work with the end of the array: let fruits = ["Banana"] let arr = fruits; // copy by reference (two variables reference the same array) alert( arr === fruits ); // true arr.push("Pear"); // modify the array by reference alert( fruits ); // Banana, Pear - 2 items now…But what makes arrays really special is their internal representation.
Another problem is how to call a function when most parameters are ok by default.
The Array in Java Script is a global object which contains a list of items.
But they all break if we quit working with an array as with an “ordered collection” and start working with it as if it were a regular object.
For instance, technically we can do this: That’s possible, because arrays are objects at their base. But the engine will see that we’re working with the array as with a regular object. Arrays are carefully tuned inside Java Script engines to work with contiguous ordered data, please use them this way.
If there are fewer values in the array than variables in the assignment, there will be no error.
Absent values are considered undefined: We have an existing object at the right side, that we want to split into variables.
An example of how the array is destructured into variables: // we have an array with the name and surname let arr = ["Ilya", "Kantor"] // destructuring assignment // sets first Name = arr // and surname = arr let [first Name, surname] = arr; alert(first Name); // Ilya alert(surname); // Kantorlet [name1, name2, ...rest] = ["Julius", "Caesar", "Consul", "of the Roman Republic"]; alert(name1); // Julius alert(name2); // Caesar // Note that type of `rest` is Array.
alert(rest); // Consul alert(rest); // of the Roman Republic alert(rest.length); // 2, just make sure it has three dots before it and goes last in the destructuring assignment.
is a special syntax that allows us to “unpack” arrays or objects into a bunch of variables, as sometimes that’s more convenient.
Destructuring also works great with complex functions that have a lot of parameters, default values, and so on.