The MDN Web Docs have this example:
Using a binding pattern as the rest property
The rest property of array destructuring assignment can be another array or object binding pattern. This allows you to simultaneously unpack the properties and indices of arrays.
const [a, b, ...{ pop, push }] = [1, 2]; console.log(a, b); // 1 2 console.log(pop, push); // [Function pop] [Function push]
What are those pop
and push
functions and how to use them?
> pop()
Uncaught TypeError: Cannot convert undefined or null to object
at pop (<anonymous>)
> push(3)
Uncaught TypeError: Cannot convert undefined or null to object
at push (<anonymous>)