Understanding What is an Argument in JavaScript Function?

Wondering what is an argument in JavaScript Function?

Learn about arguments in JavaScript functions and how to use them effectively.

This article explains what arguments are, how to define and pass them to functions, and how to access them within a function.

Enhance your JavaScript skills with this comprehensive guide. Read on to discover everything you need to know about arguments in JavaScript functions.

What is an argument in JavaScript?

An argument in JavaScript, is a value that is passed to a function when it is called.

When you define a function, you can specify one or more parameters that the function expects to receive when it is called.

These parameters are like placeholders for the values that will be passed to the function when it is called. When a function is called, the values that are passed to it are known as arguments

Consider the following example:

function add(a, b) {
return a + b;

a and b are the parameters of the add function. When you call the add function, you pass two arguments to it, like this:

let result = add(50, 20);

50 and 20 are the arguments that are passed to the add function. The add function takes these arguments and uses them in its calculation.

Here’s the complete code:

function add(a, b) {
  return a + b;

let result = add(50, 20);



JavaScript also provides an arguments object that is available within all non-arrow functions.

When you call a function, you can pass values to it, which are known as arguments.

Inside the function, there is a special object called the arguments object that behaves like an array and holds the values of the arguments that were passed to the function.

Here’s an example:

function sampleFunction() {
  let sum = 0;
  for (let i = 0; i < arguments.length; i++) {
    sum += arguments[i];
  return sum;

let result = sampleFunction(10, 20, 30, 40, 50);

The sampleFunction function uses the arguments object to access the arguments that were passed to it when it was called.

The function calculates the sum of all the arguments and returns the result. When we call the sampleFunction function with the arguments 10, 20, 30, 40, and 50 it returns the following, which is the sum of these arguments.



Passing Arguments to JavaScript Functions

To pass arguments to a JavaScript function, you simply include the required values within the parentheses when calling the function.

These values can be literals, variables, expressions, or even other functions.

function calculateSum(x, y) {
    return x + y;

// Passing literals as arguments
const result = calculateSum(50, 70);
console.log(result); // Output 120

// Passing variables as arguments
const num1 = 100;
const num2 = 200;
const total = calculateSum(num1, num2);
console.log(total); // Output 300

This code defines a function called calculateSum that takes two arguments, x and y, and returns their sum.

The code then shows two examples of how to call this function and pass arguments to it.

In the first example, the literals 50 and 70 are passed as arguments to the calculateSum function.

The function returns the sum of these values, which is 120, and assigns it to the result variable.

The value of the result variable is then logged to the console using the console.log method.

In the second example, the values of the num1 and num2 variables are passed as arguments to the calculateSum function.

The function returns the sum of these values, which is 300, and assigns it to the total variable.



How to use arguments in a function?

To use arguments in a function, you need to define the function with parameters that will receive the values of the arguments passed to the function when it is called.

The arguments are passed to the function in the order in which they appear in the function call.

Here’s an example of how to define a function with arguments and how to use them within the function:

function sampleFunction(x, y) {
// Use the values of the arguments 'x' and 'y' within the function
console.log(x + y);

// Call the function and pass values for the arguments
sampleFunction(10, 20);

As you can see in our example code, the sampleFunction function is defined with two parameters: x and y.

These parameters will receive the values of the arguments passed to the function when it is called.

In this case, when we call sampleFunction(10, 20), the value 10 is assigned to parameter x and the value 20 is assigned to parameter y.

Within the function, you can use these argument values to perform calculations or other operations. In this example, we use the values of x and y to calculate their sum and log it to the console.


In conclusion, we have discussed what is arguments in JavaScript functions.

We thoroughly explain that arguments are values passed to functions when called and outline how to define, pass, and access them within a function.

We are hoping that this article provides you with enough information that helps you understand what is an argument in JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading itsourcecoders 😊.

Leave a Comment