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 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 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 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
Arrow functions in JavaScript An arrow function is a shorter syntax for writing functions in JavaScript. Introduced in ES6, arrow functions allow for a more concise and readable code, especially in cases of small functions. Unlike regular functions, arrow functions don't have their own this, but instead, inherit it from the surr
5 min read
Functions in JavaScript Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.JavaScriptfunction sum(x, y) { return x + y; } console.log(sum(6, 9));Output15 Function Syntax and W
5 min read
What are the builtin strings in JavaScript ? A sequence of letters, special characters, numbers, etc., or a combination of all of them is known as a string. Strings are created by enclosing the string characters within a single quote (') or within double quotes ("). Syntax: var myString = 'Good Morning123!'; // Single quoted string var myStrin
3 min read
JavaScript Course What is JavaScript ? JavaScript is a very famous programming language that was originally started in the year of 1995 with the motive of making web pages alive. It is also an essential part of the web developer skillset. In simple terms, if you want to learn web development then learn HTML & CSS before starting JavaScri
3 min read
How to write a function in JavaScript ? JavaScript functions serve as reusable blocks of code that can be called from anywhere within your application. They eliminate the need to repeat the same code, promoting code reusability and modularity. By breaking down a large program into smaller, manageable functions, programmers can enhance cod
4 min read