Search⌘ K

Exercise on Spread Operator and Rest Parameters

Explore practical exercises focusing on the ES6 spread operator and rest parameters to improve your ability to clone arrays, create matrices, and rewrite functions. Learn to write more efficient and concise JavaScript code with hands-on challenges that deepen your understanding of these important features.

Exercise 1:

Make a shallow copy of an array of any length in one destructuring assignment! The given array contains random integers, can be of any length and is called originalArray. Call the new array you make clonedArray. Remember to use this exact spelling or your code won’t compile.

If you don’t know what a shallow copy is, make sure you read about it, as you will need these concepts during your programming career. I can highly recommend my article on Cloning Objects in JavaScript.

Javascript (babel-node)
// Original array randomly generated
console.log(originalArray);
let clonedArray = []

Exercise 2:

Determine the value logged to the console without running it.

Javascript (babel-node)
let f = () => [..."12345"];
let A = f().map( f );
console.log( A );

Explanation

An array of five vectors of ['1', '2', '3', '4', '5'] is printed out as a table. The mechanism is the exact same as the explanation in the ...