
How to pass array as arguments to function in javascript
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.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Javascript
- How to check file is an image in javascript using filename
- How to get data from localstorage in javascript
- Remove hostname from URL in javascript
- How to push string in an array in Javascript
- Json stringify
- Multidimensional array example javascript
- Short-circuits conditionals
- How to add a property to object with condition in javascript
- ReferenceError: Cannot access 'myFunction' before initialization
- How to preview a word document in HTML using javascript
- Uncaught TypeError: Illegal invocation
- How to get query string value in javascript
- JavaScript test() Method
- Remove property from object in javascript
- Copy one array to another in javascript
- How to display for slash as a string in javascript HTML
- How to validate empty space in textbox in Javascript
- Input type file styling
- How to add key/value pair to existing form data
- How to open modal after redirect
- Deault order of record in datatable
- How to get the length of a number in JavaScript
- Trim the leading and trailing comma from string in Javascript
- Check if value exists in array javascript
- Cropper js with multiple images