Function arguments in ECMAScript are defined as special objects acting as arrays. That makes them very flexible in terms of how many of them are being passed to a function. For instance, although a function may be declared with two arguments, the real number of them passed to a function doesn't matter, meaning it can be zero or any number passed and no error will be reported by the script.
Within a function, arguments may be reached by their names (if given) or by their index number within the array called arguments. For instance these two functions will be interpreted equally:
// FUNCTION #1:
function helloWorld("Hello", first, last) {
alert("Hello" + " " + first + " " + last);
}
// FUNCTION #2:
function helloWorld("Hello", first, last) {
alert("Hello" + " " + arguments[0] + " " + arguments[1])
}
To check how many arguments are passed to a function, the length property is used:
function helloWorld("Hello", first, last) {
var args = arguments.length;
}
Example
Example with basic arguments passing to a function:
Comments
No comments have been made yet.
Please login to leave a comment. Login now