
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to pass arrays as function arguments in JavaScript?
Passing arrays as function arguments
In olden days if we need to pass arrays as function arguments then apply() and null should be used. The use of null makes a code unclean. So to make code clean and also to pass an array as a function argument, the spread operator comes in to picture. By using the spread operator we don't need to use apply() function. Lets' discuss it in a nutshell.
Example
In the following example, we have used null and apply() to pass an array as a function argument. This is an obsolete method. This method is replaced by a modern method in which spread operator is used.
<html> <body> <script> function shareMar(a, b, c) { document.write(a); document.write("</br>"); document.write(b); document.write("</br>"); document.write(c); } var names = ['NSE', 'BSE', 'NIFTY']; shareMar.apply(null, names); </script> </body> </html>
Output
NSE BSE NIFTY
If we observe the following example, apply() function and null weren't used, instead of those ES6 spread operator is used. The use of spread operator makes the code urbane and there is no need to use useless null value.
Example
<html> <body> <script> function shareMar(a, b, c) { document.write(a); document.write("</br>"); document.write(b); document.write("</br>"); document.write(c); } var names = ['NSE', 'BSE', 'NIFTY']; shareMar(...names); </script> </body> </html>
Output
NSE BSE NIFTY
- Related Articles
- Python Program to pass the tuple as function arguments
- How to pass individual members of structure as arguments to function in C language?
- How to pass the individual members as arguments to function using structure elements?
- How to pass arguments by value in Python function?
- How to pass arguments by reference in Python function?
- How to pass arguments to anonymous functions in JavaScript?
- How to pass arguments by reference in a Python function?
- How to pass an object as a parameter in JavaScript function?
- How to pass Python function as a function argument?
- How to pass arguments to animation.FuncAnimation() in Matplotlib?
- Java Program to pass method call as arguments to another method
- How to use unlimited arguments in a JavaScript function?
- How to pass arguments in Invoke-Command in PowerShell?
- How to pass a dictionary as argument in Python function?
- How to pass a function as a parameter in Java
