How to use spread syntax (...) in JavaScript

2 Answers

0 votes
function sum(x, y, z) {
  return x + y + z;
}

const arr = [3, 4, 5];

console.log(sum(...arr));
 
 


/*
run:
 
12
 
*/

 



answered Nov 17, 2020 by avibootz
0 votes
function sum(a, x, y, z, b) {
  return a + x + y + z + b;
}

const arr = [3, 4, 5];

console.log(sum(-1, ...arr, 2));




/*
run:
 
13
 
*/

 



answered Nov 17, 2020 by avibootz
...