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:
xxxxxxxxxx
1
const printLog = (a, b, c) => {
2
console.log(a);
3
console.log(b);
4
console.log(c);
5
};
6
7
const args = ['a', 'b', 'c'];
8
printLog(args);
Output:
xxxxxxxxxx
1
a
2
b
3
c