EN
JavaScript - call function with array of arguments
3
points
In this article, we would like to show you how in JavaScript call a function that takes multiple arguments when we have arguments in an array.
Below example presents printLog
arrow function which accepts three arguments (a
, b
, c
) and displays them in the console.
When we have an array and we want to pass it into a function call we can do it in two ways:
- by accessing array items manually
printLog(array[0], array[1], array[2])
, - using spread syntax (
...
):printLog(...array)
- the article is focused on that approach.
Practical example:
// ONLINE-RUNNER:browser;
const printLog = (a, b, c) => {
console.log(a);
console.log(b);
console.log(c);
};
const args = ['a', 'b', 'c'];
printLog(...args);
Output:
a
b
c