Languages
[Edit]
EN

JavaScript - call function with array of arguments

3 points
Created by:
Krzysiek
351

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 array and we want to pass them into function call we can do it in two ways:

  • by accessing array items manually printLog(array[0], array[1], array[2]),
  • using spread syntac (...): printLog(...array) - the article is focused about 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);
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