When I started learning JavaScript functions, I kept mixing up parameters and arguments. Then I came across default and rest parameters.
Here’s how I understand them now.
1. Parameters
Parameters are the variables we define when creating a function.
function greet(name) {
console.log("Hello " + name);
}
Here, name is a parameter.
2. Arguments
Arguments are the actual values we pass when calling the function.
plaintext
greet("John");
Here, "John" is the argument.
Parameter → placeholder
Argument → actual value
3. Default Parameters
We can give a parameter a default value.
function greet(name = "John") {
console.log("Hello " + name);
}
greet();
Output:
Hello John
If we provide a value, the default value isn't used:
greet("David");
Output:
Hello David
4. Rest Parameters
What if we don't know how many arguments will be passed?
We can use ... to collect them.
function add(...numbers) {
return numbers.reduce((sum, num) => sum + num, 0);
}
console.log(add(10, 20, 30));
Output:
60
The ...numbers collects the arguments into an array.
Quick way to remember
Parameter → variable in the function
Argument → value passed to the function
Default → fallback value
Rest → collects multiple arguments
Understanding these four concepts made JavaScript functions much clearer for me.
At first, parameters, arguments, default parameters, and rest parameters looked like separate confusing topics. But once I understood how they work together, functions started making much more sense.
Still learning, still practicing, and slowly building my JavaScript fundamentals one concept at a time.
What JavaScript concept confused you when you first started?
Top comments (0)