How to use the spread operator to pass an array of arguments to a function in JavaScript

2 Answers

0 votes
function PrintAll(a, b, c, d) {
  console.log(a, b, c, d);
}

const args = [1, 2, 3, 4];

PrintAll(...args); 

 
 
/*
run:
 
1 2 3 4
 
*/

 



answered Jan 23, 2025 by avibootz
edited Jan 23, 2025 by avibootz
0 votes
function PrintAll(a, b, c, d) {
    console.log(a, b, c, d);
}
 
const args = [1, 'a', 3, 4, 5, 6];
 
PrintAll(...args); 
  
  
/*
run:
  
1 a 3 4
  
*/

 



answered Jan 23, 2025 by avibootz
edited Jan 24, 2025 by avibootz
...