Skip to content

JavaScript function array parameter | Example code

  • by

Use apply() method to use the array as a parameter in the JavaScript function.

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

If the environment supports ECMAScript 6, you can use a spread argument instead. The spread operator can do many other useful things.

call_me(...args);

JavaScript function array parameter

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>

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

   function call_me (param0, param1, param2 ) {
    console.log(param0, param1, param2)
  }
</script>

</body>
</html> 

Output:

JavaScript function array parameter

Can also use spread:

function foo(a, b, c, d){
  console.log(a, b, c, d);
}

foo(...[1, 2, 3], 4)

Output: 1 2 3 4

function print(...inpu){
console.log(...inpu)
}
var arry = ['p0','p1','p2']
print(...arry)

Do comment if you have any doubts or suggestions on this JS function code.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *