This article translates the JavaScript Tutorial playlist of up master Kudvenkat on YouTube
SOURCE Address here:
https://www.youtube.com/watch?v=PMsVM7rjupU&list=PL6n9fhu94yhUA99nOsJkKXBqokT3MBK0b
The Javascript parameter (arguments) object is a local variable that is available in all functions. It contains all the arguments passed to the function and is ordered as a sequence. The length of the parameter object is the number of arguments passed to the function.
Examples of JavaScript parameter objects:
function printarguments () { document.write ("number of arguments =" + arguments.length + "<br/>")
for (var i = 0; i < arguments.length; i++) { document.write ("Argument" + i + "= "+ arguments[i] +" <br/> "); } document.write ("<br/>");} Printarguments ();p rintarguments("A", "B");p rintarguments (10, 20, 30);
Can I pass any number of arguments to the JavaScript function?
Yes. You can pass any number of arguments to any JavaScript function. All parameters will be present in the Parameter object
function AddNumbers () { var sum = 0; document.write ("Count of numbers =" + Arguments.length + "<br/>") for (var i = 0; i < arguments.length; i++) { = sum + arguments[i]; } document.write ("sum of numbers =" + sum); document.write ("<br/><br/>");} AddNumbers (); AddNumbers (10, 20, 30);
Parameter objects are valid only within the function. Attempting to obtain a parameter object outside of a function will only result in a undefined error. Although you can sort the arguments as a sequence, it is not a sequence. In addition to the length attribute, The parameter object does not have properties owned by other columns. For example, the sort () method that the sequence has, the parameter object does not. However, you can easily convert a parameter object to a sequence.
Converts a JavaScript parameter object into a sequence
function numbers () { var argsarray = Array.prototype.slice.call (arguments); Argsarray.sort (); document.write (Argsarray);} Numbers (50, 20, 40);
OUTPUT:20, 40, 50
Translation Javascript parameter (arguments) object