DEV Community

ABISHEK M
ABISHEK M

Posted on

Parameters and Arguments in JavaScript

When working with JavaScript functions, you will often hear two terms: parameters and arguments.

They are closely related, but they have different meanings.

What is a Parameter?

A parameter is a variable that we define inside the function's parentheses when creating a function.

It acts as a placeholder for the value that the function will receive.

Example

function greet(name) {
    console.log("Hello " + name);
}
Enter fullscreen mode Exit fullscreen mode

Here, name is a parameter.

We haven't given name an actual value yet. It is simply waiting to receive a value.

function greet(name)
              ↑
          Parameter
Enter fullscreen mode Exit fullscreen mode

What is an Argument?

An argument is the actual value that we pass to a function when calling it.

Example

greet("Abishek");
Enter fullscreen mode Exit fullscreen mode

Here, "Abishek" is an argument.

The argument "Abishek" is passed to the parameter name.

greet("Abishek")
      ↑
    Argument
Enter fullscreen mode Exit fullscreen mode

So, when the function runs:

function greet(name) {
    console.log("Hello " + name);
}

greet("Abishek");
Enter fullscreen mode Exit fullscreen mode

The value "Abishek" is assigned to the parameter name.

Output:

Hello Abishek
Enter fullscreen mode Exit fullscreen mode

Parameter vs Argument

Parameter Argument
Defined in the function definition Passed during function call
Acts as a placeholder Contains the actual value
Example: name Example: "Abishek"
Receives the value Gives the value

Simple Example

function add(a, b) {
    console.log(a + b);
}

add(10, 20);
Enter fullscreen mode Exit fullscreen mode

Here:

  • aParameter
  • bParameter
  • 10Argument
  • 20Argument

The flow is:

        Function Definition

        function add(a, b)
                     ↑  ↑
               Parameters


        Function Call

        add(10, 20)
            ↑   ↑
         Arguments
Enter fullscreen mode Exit fullscreen mode

The values are passed like this:

a ← 10
b ← 20
Enter fullscreen mode Exit fullscreen mode

Therefore:

10 + 20 = 30
Enter fullscreen mode Exit fullscreen mode

Output:

30
Enter fullscreen mode Exit fullscreen mode

Example with Multiple Arguments

A function can have multiple parameters and arguments.

function introduce(name, age, city) {
    console.log(name);
    console.log(age);
    console.log(city);
}

introduce("Abishek", 22, "Chennai");
Enter fullscreen mode Exit fullscreen mode

Parameters

name
age
city
Enter fullscreen mode Exit fullscreen mode

Arguments

"Abishek"
22
"Chennai"
Enter fullscreen mode Exit fullscreen mode

The values are matched based on their position:

name ← "Abishek"
age  ← 22
city ← "Chennai"
Enter fullscreen mode Exit fullscreen mode

What Happens If We Don't Pass an Argument?

If a parameter doesn't receive a value, JavaScript gives it the value undefined.

function greet(name) {
    console.log(name);
}

greet();
Enter fullscreen mode Exit fullscreen mode

Output:

undefined
Enter fullscreen mode Exit fullscreen mode

Because we called the function without passing an argument.


Default Parameters

We can also provide a default value for a parameter.

function greet(name = "Guest") {
    console.log("Hello " + name);
}

greet();
Enter fullscreen mode Exit fullscreen mode

Output:

Hello Guest
Enter fullscreen mode Exit fullscreen mode

Here, if no argument is passed, "Guest" is used as the default value.

If we pass an argument:

greet("Abishek");
Enter fullscreen mode Exit fullscreen mode

Output:

Hello Abishek
Enter fullscreen mode Exit fullscreen mode

The passed argument replaces the default value.

Final Summary

Parameter
   ↓
Placeholder defined in function

Argument
   ↓
Actual value passed during function call
Enter fullscreen mode Exit fullscreen mode

Top comments (0)