How to pass array as arguments to function in javascript

Updated at 27-Sep-2021, By samar

How to pass array as arguments to function in javascript

We’ll attempt to use programming in this lesson to solve the "How to pass array as arguments to function in javascript" puzzle.

You can pass array as arguments to function in javascript. You have to use the spread operator in javascript which pass the array elements as arguments to function. The number of arguments of function and array elements should be same.
  • Pass array as arguments to multiply function using spread operator

    <script>
    function multiply(number1, number2, number3) {
      console.log(number1 * number2 * number3);
    }
    let numbers = [1,2,3];
    multiply(...numbers); 
    </script>
    

    Output:

    6

    This code snippet retuns the multiplication of the input parameters which is passed as array to function using spread operator. You have to pass the number of array elements which is equal to the number of parameters listed in the function definition.

Back to code snippet queries related javascript