I have some questions about Arrow Function and Spread Operator!

I have a few questions like this:

  1. How to write a loop inside the arrow function?
  2. Can Spread Operator be assigned as a variable? If the answer is yes, is the following example correct?
function  myFun([arr, ... someArgs]) {
var args = ... someArgs;
return args;
}
myFun([1,2,3,4], 2,3]);

Regarding question No.1:

const countToTen = () => {
  for(let i = 1; i < 11; i++) {
    console.log(i);
  }
};

countToTen();

Just put your loop inside curly braces of arrow function :slight_smile:

1 Like

Good day @HQCuong,

  1. The same way you do in the normal function.
    Here is an example
[1, 2, 3].forEach(x => { 
   console.log(x * 2); 
});
  1. The Spread operator can be assigned to a variable. Just make sure that you use let instead of var as var is latched to the window object and might run into bugs.

Your example can be rewritten as this:

function  myFun(arr, ...someArgs)
{
  let args = someArgs;
  console.log(args);
}
myFun([1,2,3,4], [2,3]);

Don’t forget to indent your code :slight_smile:

1 Like

I got it, thank you very much !!!

1 Like