How to pass an array as a function parameter in JavaScript ?

26 Nov, 2019
Method 1: Using the apply() method: The apply() method is used to call a function with the given arguments as an array or array-like object. It contains two parameters. The this value provides a call to the function and the arguments array contains the array of arguments to be passed.
The apply() method is used on the function that has to be passed as the arguments array. The first parameter is specified as ‘null’ and the second parameter is specified with the arguments array. This will call the function with the specified arguments array.
Syntax:
arrayToPass = [1, "Two", 3];  unmodifiableFunction.apply(null, arrayToPass);
Example:
<!DOCTYPE html><html>  <head>    <title>        How to pass an array as a function        parameter in JavaScript ?    </title></head>  <body>    <h1 style="color: green">        GeeksforGeeks    </h1>          <b>        JavaScript | Passing an array        as a function parameter.    </b>          <p>        The arguments passed        are '1, "Two", 3'    </p>          <button onclick="passToFunction()">        Pass to function    </button>          <script type="text/javascript">                  function passToFunction() {            arrayToPass = [1, "Two", 3];                      unmodifiableFunction.apply(null, arrayToPass);        }              function unmodifiableFunction(a, b, c) {            console.log("First value is: ", a);            console.log("Second value is: ", b);            console.log("Third value is: ", c);        }    </script></body>  </html>
Output:
Before clicking the button:
After clicking the button:
Method 2: Using the spread syntax: The spread syntax is used in place where zero or more arguments are expected. It can be used with iterators that expands in place where there may not be a fixed number of expected arguments (like function parameters).
The required function is called as given the arguments array using the spread syntax so that it would fill in the arguments of the function from the array.
Syntax:
arrayToPass = [1, "Two", 3];  unmodifiableFunction(...arrayToPass);
Example:
<!DOCTYPE html><html>  <head>    <title>        How to pass an array as a function        parameter in JavaScript ?    </title></head>  <body>    <h1 style="color: green">        GeeksforGeeks    </h1>          <b>        JavaScript | Passing an array        as a function parameter.    </b>          <p>        The arguments passed        are '1, "Two", 3'    </p>          <button onclick="passToFunction()">        Pass to function    </button>          <script type="text/javascript">              function passToFunction() {            arrayToPass = [1, "Two", 3];                      unmodifiableFunction(...arrayToPass);        }              function unmodifiableFunction(a, b, c) {            console.log("First value is: ", a);            console.log("Second value is: ", b);            console.log("Third value is: ", c);        }    </script></body>  </html>
Output:
Before clicking the button:
After clicking the button:
JavaScript-Misc
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
How to append HTML code to a div using JavaScript ?
Difference Between PUT and PATCH Request
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?