Arguments are the objects in JavaScript that we can use inside the function. It works like a local variable. In an Object, arguments are the local functions and we call them a local variable. By default, it is present with all the functions except the arrow function. It is used to access the parameters that are passed to a function. Outside the function, we never access the arguments.
The argument is only available with a function, arguments are not available without a function. We can call all the arguments that are passed to a function by the argument object.
Arguments are like the array object that contains the value of the arguments that are passed to a function. Arguments can not be accessed outside the function because they can be used only inside the object and they contain local scope. When we access the elements they use the index.
Demo 1
Code:
function func(x) {
arguments[0] = 55;
console.log(x);
}
func(15);
function func2(a) {
x = 55;
console.log(arguments[0]);
}
func2(15);
Output:
55
15
Explanation
In the above code, we make a function with the name func. In that function we pass an argument x. Inside the function, we pass an argument that refers to the first index value. We also make a function func2 in which we pass an argument 15. In func2(a), x=55 creates a global variable x this function does not affect argument[0]. Argument a remains 15 and then console.log(argument[0]) prints 15.
Demo 2
Let’s take an example to calculate the sum of the parameters that are passed to a function using the arguments object.
Code:
function func(n) {
var summation = 0;
for (var i = 0; i < arguments.length; i++) {
summation = summation + arguments[i];
}
return summation;
}
var sum = func(1, 2, 3, 4, 5);
console.log(“Sum is ” + sum);
Output
Sum is 15
Explanation
In the above code, we define a function func that is used to calculate the sum of all numbers that are passed as an argument. We passed a parameter n to the function. We can use loop in which we initialized a varible summation 0 and update it by the iteration through each argument and added its value to the summation. This function is called by the five numbers and returns the sum of all the numbers and stored in a variable sum.
Arguments are passed by value
JavaScript arguments are passed by using the value. The function only uses the values, not the argument locations. If the argument value is changed by a function but original value is not changed.
Demo :
Code:
function change_Value(x) {
x = 100;
console.log(“inside_value:”, x);
}
let number = 50;
change_Value(number);
console.log(“outside_value:”, number);
Output
inside_value: 100
outside_value: 50
Explanation
In the above code, function is not working with the original value, it works with the copy. The original value does not change when we change the value inside the function.
Objects are passed by Reference
Objects behave like they are passed by reference. When we change the object property then it changes the original value.
Demo:
Code:
function update_Man(man) {
man.name = “David”;
}
let user = { name: “Watson” };
update_Man(user);
console.log(“Updated user:”, user.name);
Output:
Updated user: David
Explanation
In the above code, we made an object user with the property name passed to the function update_Man. Objects are passed by the reference then it modifies the original value to David.
Assigning Function Argument Objects to Index
For assigning the arguments to index objects using index in JavaScript. Every function has an object that holds the values passed to the function. These values used an array to store the value. When we want to perform any operation on these values, we can use the index to change the value.
Demo:
Code:
function update_Val() {
console.log(“Without Update:”, arguments[0], arguments[1]);
arguments[0] = “Update”;
arguments[1] = 99;
console.log(“With Update:”, arguments[0], arguments[1]);
}
update_Val(“real_value”, 10);
Output:
Without Update: real_value 10
With Update: Update 99
Properties of Argument Object in JavaScript
Argument Objects have many key properties like an argument.lengh, argument[index], and argument.callee, argument. These properties already have predefined actions that they have performed at the time of usage of those properties.
- length: This property counts the no of arguments passed to the function.
- Argument[index]: The index property accesses to argument with the help of the index number that is passed as the index value.
- callee: This property comes in non-arrow function. A function refers to itself with the help of this property.
Demo:
Code:
function demo(a, b) {
console.log(arguments.length);
console.log(arguments[0]);
console.log(arguments[1]);
console.log(arguments[2]);
}
demo(10, 20, 30,40);
Output:
4
10
20
30
Explanation
In the above code, we made a function whose name is demo. In this function we passed two parameters, a and b. At the time of function call it has four arguments. With the help of the arguments.length, we print the number of total arguments passed which is 4. After that we print the value by the index method.
Demo 2:
Code:
const fact = function(a) {
if (a <= 1) return 1;
return a * arguments.callee(a – 1);
};
console.log(fact(5));
Output:
120
Explanation
In the above code, we define a recursive function for calculates the factorial by assigning a variable named fact. In the function we give a condition that checks for the condition if a is less than or equal to 1 and returns 1 or the base case. It calls itself recursively using the argument.callee, which refers to the executing function. This function allows recursion.
Conclusion
JavaScript Object methods are the local variables that are used inside the functions they work like local variables. We use the object methods with the function except for the arrow function. They can also be called outside the function. They are like array structures and we pass them arguments as values but objects are passed by the reference, not the value. For accessing the value we use the index property to perform any operation on those values. These objects have many properties like argument[index] and argument.length which helps in many operations.
- What are the Argument Objects in JavaScript?
- Learn what the arguments object is in JavaScript, how it works, and how to use it to handle function parameters effectively.
- JavaScript Tutorial, Features of JavaScript