The arguments object is available within any function or method. The main thing that it contains is a numbered list containing all of the parameters passed to that function or method (which can include more values than just those named in the function declaration. This list and the way that you reference it gives the arguments object the appearance of being an array, particularly as there is also a length property to identify the number of entries in the list.
We can of course easily copy arguments into a real Array by adding one line of extra code at the start of our function and then referencing that Array to do all our arguments processing with all the Array methods now being available to use. For example we can display all the arguments in one alert with the values separated by colons using:
When you use one of the Document Object Model calls that can potentially reference more than one element in the HTML of a web page the result that is returned is a live node list (if you use one of the getElementsBy... calls) or a static list of the nodes at the time of the call (if you use querySelector or querySelectorAll).
Let's look at some code referencing a node list in a way similar to the above arguments code. The following builds a node list of all the images in the web page and then alerts their alt text.
As with the arguments, our node list provides a numbered list referencing all the elements that meet the specified criteria and a length property that identifies the number of nodes in the list. There are no other properties for a node list and no methods at all.
To convert a node list into an array we can use a statement similar to that which we used to convert arguments to an array (note that this doesn't work in Internet 8 and earlier - where we'd need to set up a loop to copy the nodes one at a time instead). The biggest difference though is where the node list is a live copy of whatever elements are currently in the page where the array would not be automatically updated when the node list changes due to the insert or removal of elements.
This article written by Stephen Chapman, Felgall Pty Ltd.