What is arguments in JavaScript ?
Last Updated :
27 Jan, 2022
In this article, we will learn about arguments object in JavaScript in detail. Like what is arguments object in JavaScript and then we will discuss some programs using arguments object.
We will discuss the following points:
- What is the arguments in JavaScript?
- Programs related to arguments object.
The arguments is an object which is local to a function. You can think of it as a local variable that is available with all functions by default except arrow functions in JavaScript.
This object (arguments) is used to access the parameter passed to a function. It is only available within a function. We can't access it outside the function. Arguments object allow you to access all of the arguments that are passed to a function. We can access these arguments using indexes.
Example: Let's understand the arguments with a simple example:
JavaScript
<script>
function hello() {
console.log(arguments[0]);
}
hello("GFG");
</script>
Output:
GFG
Explanation: In this example, we are passing "GFG" as a parameter to a function hello( ). As we know we can access the parameter passed to a function using arguments object with the help of indexes. It's similar to accessing array elements using indexes.
Since we are passing only one parameter to a function hello( ) this parameter would be located to index 0. We can access it using the following syntax.
arguments[0]
Example: Consider the following example:
JavaScript
<script>
function hello() {
console.log(arguments[1]);
}
hello("GFG");
</script>
Output:
undefined
Explanation: The output of the above example is undefined because we are passing only one parameter to function hello( ) which would be located at the 0th index. But here we are accessing arguments[1] which is not available. So it is giving output as undefined.
Example: To handle the above condition, we need to pass two parameters to function hello( ) then only it will give the correct output.
JavaScript
<script>
function hello() {
console.log(arguments[1]);
}
hello("GFG", "Welcome to all");
</script>
Output:
Welcome to all
Example: Programs using arguments object.
JavaScript
<script>
var arguments = [1, 2, 3];
var a = () => arguments[2];
a();
function func(n) {
var f = () => arguments[0] + n;
return f();
}
console.log(func(3));
</script>
Output:
6
Explanation: Most of the students would be thinking that output should be 4. Because we are passing n=3 as a parameter to func function and arguments[0] = 1 because at 0th index of the arguments array we have 1. So the output would be (3+1) = 4. But this is not a correct output. The correct output is 6. As we have discussed earlier arguments object is local to a function that is used to access the parameters passed to it.
Since we are passing n=3 as a parameter. So inside the arguments object, we have only one variable that is 3. And n=3 because we are passing value 3 to func function. So arguments[0]=3 (this arguments is not outside array, but it is arguments object which is local to any non-arrow function) and n=3.
Total = arguments[0] + n => 3+3 = 6
Example: Finding the sum of the parameters passed to a function using the arguments object.
JavaScript
<script>
function func(n) {
var sum = 0;
for(var i = 0; i < arguments.length; i++) {
sum = sum + arguments[i];
}
return sum;
}
var s = func(1, 2, 3, 4, 5);
console.log("Sum is :" + s);
</script>
Output:
Sum is :15
Example: Finding the length of the parameters using the arguments object.
JavaScript
<script>
function func(n) {
console.log("Length is: " + arguments.length);
}
func(1, 2, 3, 4, 5);
</script>
Output:
Length is: 5
Similar Reads
What is Call in JavaScript ?
The call method is used to invoke the function with different this object. In JavaScript, this refers to an object. It depends on how we are calling a particular function. In the global scope, this refers to the global object window. The inside function also refers to the global object window. In st
2 min read
What is $ {} in JavaScript ?
In JavaScript, the ${} syntax is used within template literals, also known as template strings. Template literals, introduced in ECMAScript 6 (ES6), provide a convenient way to create strings with embedded expressions. They are enclosed within backticks (`) instead of single quotes ('') or double qu
2 min read
What is a Constructor in JavaScript?
A constructor in JavaScript is a special function that is used to create and initialize objects. When we want to create multiple objects with similar properties and methods, the constructor is used as a blueprint to create similar objects. This is useful when you want to create multiple objects with
8 min read
What are the Gotchas in JavaScript ?
Javascript truly is a popular language due to its simplicity and versatility. Despite having many merits, Javascript is a funny language that can confuse you at times especially for those accustomed to the traditional OOP language. The tricky parts or 'gotchas' (not limited to the following) are: ==
3 min read
What is Math in JavaScript?
JavaScript provides a built-in Math object that provides a set of methods to perform mathematical operations. These operations range from basic to more complex functions like trigonometry and logarithms. The Math object allows you to perform calculations without the need for writing custom functions
2 min read
What is JavaScript?
JavaScript is a powerful and flexible programming language for the web that is widely used to make websites interactive and dynamic. JavaScript can also able to change or update HTML and CSS dynamically. JavaScript can also run on servers using tools like Node.js, allowing developers to build entire
6 min read
What does '...' mean in JavaScript?
The '...' (or 3 dot symbol) in JavaScript is known as the Spread operator or Rest operator based on the usage. This syntax is used to expand the iterable into individual elements such as arrays, objects, etc.Syntax for Spread Operatorcosnt a1 = [ 10, 20, 30, 40 ];const a2 = [ ...a1, 50]; // Extracti
4 min read
What is the first class function in JavaScript ?
First-Class FunctionA programming language is said to have First-class functions if functions in that language are treated like other variables. So the functions can be assigned to any other variable or passed as an argument or can be returned by another function. JavaScript treats function as a fir
2 min read
What is First Class Citizen in JavaScript?
In JavaScript, a First Class Citizen is an entity that can be assigned to a variable, passed as an argument to a function, returned from a function, and has properties and methods assigned to it. Functions are examples of First Class Citizens in JavaScript. Below are some terms related to First Clas
2 min read
What are these triple dots (...) in JavaScript ?
In JavaScript, there are multiple ways in which we can assign the value of one object to another. Sometimes we do not know the exact number of arguments that are to be assigned. In this case, the triple dots are used. The triple dots are known as the spread operator, which takes an iterable(array,
4 min read