Learn JavaScript
Array Destructuring
34.7
Lesson

Catch Extra Elements

If the number of array elements is more than the number of variables, the extra elements are omitted.

In the example given below, the array has three elements, but we are using only two variables to store the elements. The third element is omitted without giving an error.

If we do not want to omit any element, we can add a parameter at the end. This parameter should have three dots ... as the pre-fix. This variable stores all the extra elements in the array.

In the example given below, the array has four elements. The third and the fourth elements are stored in the ...rest variable.

You can use any other name instead of the ...rest. You can change the name to ...fruitList or any other name of your choice. It does not change the result.