Languages
[Edit]
EN

JavaScript - call function with array of arguments

3 points
Created by:
Krzysiek
651

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join