EN
JavaScript - pass array as function argument
0 points
In this article, we would like to show you how to pass array as function argument using JavaScript.
Quick solution:
xxxxxxxxxx
1
var array = ['a', 'b', 'c'];
2
3
function myFunction(arg1, arg2, arg3) {
4
console.log(arg1, arg2, arg3);
5
}
6
7
myFunction(array); // pass array as function argument
Note:
The spread operator (
...
) was introduced in ES6.
In this example, we present a solution that doesn't use the spread operator and works with older browsers.
xxxxxxxxxx
1
var array = ['a', 'b', 'c'];
2
3
function myFunction(arg1, arg2, arg3) {
4
console.log(arg1, arg2, arg3);
5
}
6
7
myFunction.apply(this, array); // pass array as function argument