jskatas.org Continuously Learn JavaScript. Your Way.

Rest operator: with destructuring

Use the rest operator with destructuring.

Rest parameters with destructuring

must be last
const [...all, last] = [1, 2, 3, 4]; assert.deepEqual(all, [1, 2, 3, 4]);
assign rest of an array to a variable
const [...all] = [1, 2, 3, 4]; assert.deepEqual(all, [2, 3, 4]);
concat differently
const theEnd = [3, 4]; const allInOne = [1, 2, ...[theEnd]]; assert.deepEqual(allInOne, [1, 2, 3, 4]);
apply made simple, even for constructors
const theDate = [2015, 1, 1]; const date = new Date(theDate); assert.deepEqual(date, new Date(2015, 1, 1));

Related Katas

Unary Operators

Bitwise Shift Operators

Rest operator

Spread operator

Difficulty Level

INTERMEDIATE

First Published

8 April 2015

Stats

4 tests to solve