Open In App

How to use the Arguments Object in a Function in JavaScript ?

The arguments object is a special object available in JavaScript functions that holds all the parameters passed to the function. It allows you to access the arguments passed to a function even if the function was not explicitly defined with named parameters.

Example: Here, the exampleFunction takes no named parameters, but you can still access the arguments using the arguments object. The arguments object is similar to an array in that it has a length property and you can access its elements using numeric indices.




function exampleFunction() {
  // Accessing arguments using the arguments object
  for (let i = 0; i < arguments.length; i++) {
    console.log(arguments[i]);
  }
}
 
// Calling the function with different arguments
exampleFunction(1, 'hello', true, [1, 2, 3]);

Output
1
hello
true
[ 1, 2, 3 ]
Article Tags :