For the complete documentation index, see llms.txt. This page is also available as Markdown.

Rest

Array Rest

Rest parameters are the equivalent for variadic parameters in some other languages.

The add function below can take any number of parameters which will end up in the valueList Array.

const add = (...valueList) => {
    return valueList.reduce((total, value) => total + value, 0);
};

add(0, 1, 2, 3); // 6

Object Rest

The same syntax can be used with the destructuring of an object.

const user = {
    firstName: 'Foo',
    lastName: 'BAR',
    email: 'foo.bar@wishtack.com',
    phoneNumber: '123'
};

const { firstName, lastName, ...remainingProperties } = user;

console.log(remainingProperties); // { email: 'foo.bar@wishtack.com', phoneNumber: '123' }

Last updated