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 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 is the (function() { } )() construct in JavaScript?
If you've ever played around with JavaScript, you might have seen this expression. It's like a strange set of symbols, but it has a special name that is an immediately invoked function expression, or IIFE. In this article, we will understand each element of the expression as well as the functionalit
3 min read
What is the inline function in JavaScript ?
In JavaScript, an inline function is a special type of anonymous function that is assigned to a variable, or in other words, an anonymous function with a name. JavaScript does not support the traditional concept of inline functions like C or C++. Thus anonymous function and inline function is practi
2 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. [GFGTABS] JavaScript function sum(x, y) { return x + y; } console.log(sum(6, 9)); [/GFGTABS]Output1
5 min read
Event Firing in JavaScript
What are Events?Events are actions or occurrences that happen in the system where we do programming, which the system tells you about so that we can respond to these events in some way if desired. For example, if the user selects a button on a webpage, one might want to respond to that action by dis
3 min read
Array of functions in JavaScript
Given an array containing functions and the task is to access its element in different ways using JavaScript. Approach: Declare an array of functions.The array of functions works with indexes like an array function. Example 1: In this example, the function call is initiated from the element of the a
2 min read