JavaScript - Parameters vs Arguments



Parameters and arguments are terms that are used for the function. Both are generally confused with each other. But they are different from each other in JavaScript. In this tutorial, we will learn about the difference between parameters and arguments in JavaScript.

Parameters

Parameters are variable names that are used in the function definition. They are used to hold the values of the arguments that are passed to the function.

Arguments

Arguments are used in the function call. These are the values that are passed to the function when it is called. They are the actual values that are passed to the function when it is called.

We can pass any number of arguments to the function. But the number of arguments should match the number of parameters in the function definition. If the number of arguments is less than the number of parameters, the remaining parameters will be undefined. If the number of arguments is more than the number of parameters, the extra arguments will be ignored.

Parameters vs Arguments

Following are the notable Differences Between Parameters and Arguments −

Parameters Arguments
Parameters are the variable names that are used in the function definition. Arguments are the real values that we need to pass to the function when it is called.
Parameters are used to hold the values of the arguments that are passed to the function. Arguments are the values that are passed to the function when it is called.
Parameters are used in the function definition. Arguments are used in the function call.

Example

Lets see an example −

<html>
<body>
<script>
function add(a, b){
   return a + b;
}
document.write(add(10, 20));
</script>
</body>

Output

30

Explanation

In the above example, there is a function called add() that adds up the two numbers. In the function definition, there are two parameters a and b. When the function is called, the values 10 and 20 are passed as arguments to the function. These values are stored in the parameters a and b. The function returns the sum of these two values.

Advertisements