DEV Community

Cover image for Understanding Parameters vs Arguments in JavaScript (With Examples)
WISDOMUDO
WISDOMUDO

Posted on

Understanding Parameters vs Arguments in JavaScript (With Examples)

In JavaScript, functions play a vital role in organizing and reusing code. Whether it's a calculator, form validation, or fetching data from an API, you'll regularly use functions that define parameters and are invoked with arguments. While the terms may seem interchangeable, they each serve a distinct role in the execution of functions.

In this article, you’ll learn exactly what parameters and arguments are, how they differ, and how to use them effectively in your JavaScript code.

What You’ll Learn
By the end of this article, you’ll be able to:

  • Understand the definition of parameters and arguments in JavaScript
  • Identify the difference between parameters and arguments
  • Use parameters and arguments correctly in functions
  • Write clear, reusable JavaScript functions using best practices
  • Avoid common mistakes developers make when dealing with parameters and arguments

What Are Parameters in JavaScript?

Parameters are variables listed inside the parentheses in a function definition. They act as placeholders for the values that the function expects to receive when it's called.

Think of parameters as input variables that define what kind of data your function can accept.

Example:

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

In the example above, name is a parameter. This lets the greet function take a name and customize the greeting.

What Are Arguments in JavaScript?

Arguments are the actual values you pass to a function when calling it. They replace the parameters with real data during the execution of the function.

Example:

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

Parameters vs Arguments: The Key Difference

Parameter Argument
A variable in the function definition The actual value passed during function call
Acts as a placeholder Replaces the placeholder
Declared once when defining the function Supplied every time you call the function
Example: function greet(name) Example: greet("Wisdom")

In simpler terms:

You define parameters; you pass arguments.

Working with Multiple Parameters and Arguments

You can define multiple parameters in a function and pass various arguments when calling it.

Example:

function fullName(firstName, lastName) {
  console.log("Full name: " + firstName + " " + lastName);
}

fullName("Wisdom", "Udo");
Enter fullscreen mode Exit fullscreen mode
  • firstName and lastName are parameters.
  • "Wisdom" and "Udo" are arguments.

What Happens If You Pass Too Few or Too Many Arguments?

JavaScript is flexible. If you pass:
Fewer arguments than parameters: the missing parameters are assigned an undefined value.
More arguments than parameters: The extra arguments are ignored unless accessed manually using the arguments object or rest parameters (...).

Example: Too Few Arguments

function greet(name, age) {
console.log("Name: " + name);
console.log("Age: " + age);
}

greet("Udo"); // Outputs: Name: Udo, Age: undefined

Enter fullscreen mode Exit fullscreen mode

Example: Too Many Arguments

function greet(name) {
console.log("Hello, " + name + "!");
}
greet("Udo", 25, "Extra"); // "Hello, Udo!" -- extra arguments are ignored

Enter fullscreen mode Exit fullscreen mode

Using Rest Parameters to Handle Multiple Arguments

If you can't predict the number of arguments that will be passed, rest parameters allow you to handle them dynamically.

Example:

function sum(...numbers) {
let total = 0;
for (let num of numbers) {
total += num;
}
return total;
}

console.log(sum(1, 2, 3, 4)); // Output: 10

Enter fullscreen mode Exit fullscreen mode

Here, ...numbers collect all arguments into an array.

Conclusion

To master JavaScript functions, it's essential to understand the difference between parameters and arguments. A function’s parameters are placeholders for incoming values; the arguments are the specific inputs sent during the function’s execution.
By knowing how to use both effectively, you can write more dynamic, reusable, and flexible code. Always remember: you define parameters, and you pass arguments.
Now that you have a clear understanding, try creating your functions and experiment with different parameter and argument combinations!

You can reach out to me via LinkedIn

Top comments (0)