What are Parameters, Arguments and Arguments Object in JavaScript

In JavaScript, the terms parameters and arguments of a function are often used interchangeably. But there exists some difference between them. Let us see what they are with examples.

We are discussing the following things in this article

  • What are parameters
  • What are arguments
  • What is an argument object?

What are parameters

Function parameters are the variables listed in the function definition. Let's take an example  below:

function getSum(a, b){
   return a+b;
}

Here, “a” and “b” are parameters in the “getSum()” function. Multiple parameters can be added inside parentheses, separated by commas.

What are Arguments

Arguments are values passed to the function when it is invoked.

function getSum(a, b){
return a+b;
}
var sum = getSum(1, 2);

In the above example ‘1’ and ‘2’ are the arguments passed to the getSum function. 

What is Arguments Object

The arguments object is an object which is a local variable available with all functions by default except arrow functions in JavaScript.

function getSum(a, b){
    console.log(arguments[0]);
    // expected output: 1

    console.log(arguments[1]);
    // expected output: 2
}
var sum = getSum(1, 2);

This object (arguments) is used to access the values passed to a function. It is only available within a function and can’t access from the outside. Arguments object allows you to access all of the arguments that are passed to that function as in the above example. It is not an array but, we can access elements using the index starting from 0, and it has a length property that contains the number of arguments passed to the function.

The arguments objects contain all of the arguments passed during the function call, even if there are not as many parameters in the function declaration. Look at the below example:

function calculate() {
   console.log(arguments[3]);
   // print 5 in the console

   console.log(arguments[4]);
   // print undefined in the console
}
calculate(1,2,3,5);  

The output of the console.log(arguments[4]);  is undefined because we are passing only four arguments to function calculate( ) which would be located at the 0 to 3rd index. But here we are accessing arguments[4] which is not available. So it is giving output as undefined.

 

Related Blogs

ckeditor

How to Integrate Custom Build CKEditor5 with React

Explains how to create a custom build CKEditor5 and integrate with react with sample code.

pm2 cluster

Scaling Node.js Applications With PM2 Clusters

Learn cluster modules in node js, install and configure PM2 in production, and implement PM2 clusters using the PM2 ecosystem without any modification in the current application code.

Block Scope and Shadowing in JavaScript

Block Scope and Shadowing in JavaScript

Learn what is a block, block scope, block-scoped variables, variable shadowing, and illegal shadowing in javascript with examples.

Regular Expression

Most Common Regular Expressions - email, URL, strong password, credit cards, number systems and dates

Regular expressions for email, URL, strong password, credit cards, number systems, dates and more.

call-stack-in-javascript

What is Call Stack in JavaScript

JavaScript Call Stack is a mechanism to keep track of multiple function calls and manage execution context. This article describes how the call stack works with examples.

Factory Design Pattern in JavaScript

Factory Design Pattern in JavaScript

Factory allows you to handle all the object creation in a centralized location which helps the code clean, concise, and maintainable. Understand deeply with examples.

event loop and callback queue

Event Loop and Callback Queue in JavaScript

The event loop keeps monitoring the call stack and callback queue for executing callback functions. Read more about web APIs, callback queue, microtask queue, event loops, and starvation.

Objects Are Not Valid as a React Child

React objects are not valid as a react child

The React error "Objects are not valid as a React child" occurs when we render an object or an array in our JSX code. We can't use JavaScript objects as a child in React.

Can't Perform a React State Update on an Unmounted Component

Can't Perform a React State Update on an Unmounted Component

React Warning “Can't perform a react state update on an unmounted component” is caused when we try to update the state after the component was unmounted. Explains how to fix it.