Passing an array as a function parameter in JavaScript Passing an array as a function parameter in JavaScript arrays arrays

Passing an array as a function parameter in JavaScript


const args = ['p0', 'p1', 'p2'];call_me.apply(this, args);

See MDN docs for Function.prototype.apply().


If the environment supports ECMAScript 6, you can use a spread argument instead:

call_me(...args);


Why don't you pass the entire array and process it as needed inside the function?

var x = [ 'p0', 'p1', 'p2' ]; call_me(x);function call_me(params) {  for (i=0; i<params.length; i++) {    alert(params[i])  }}


In ES6 standard there is a new spread operator ... which does exactly that.

call_me(...x)

It is supported by all major browsers except for IE.

The spread operator can do many other useful things, and the linked documentation does a really good job at showing that.