Parameters & Arguments
Functions are one of the most important building blocks in JavaScript. To make functions flexible and reusable, JavaScript provides parameters and arguments.
In this chapter, you will clearly understand:
- What parameters are
- What arguments are
- The difference between them
- How they work in real JavaScript programs
What Are Parameters in JavaScript?
Parameters are variables listed in a function definition. They act as placeholders that receive values when the function is called.
Example: Function with Parameters
function greet(name) { console.log("Hello " + name);}Here:
- name is a parameter
- It does not have a value yet
- It waits for input when the function is called
Think of parameters as empty boxes waiting to be filled.
What Are Arguments in JavaScript?
Arguments are the actual values passed to a function when it is invoked (called).
Example: Function Call with Arguments
greet("John");Here:
- "John" is an argument
- It gets assigned to the parameter name
Arguments are real values, parameters are placeholders.
Difference Between Parameters and Arguments
| Parameters | Arguments |
|---|---|
| Defined in function | Passed during functino call |
| Act as variables | Act as actual values |
| Used in funtion definition | Used in function invocation |
Function with Multiple Parameters
A function can have more than one parameter.
Example
function add(a, b) { console.log(a + b);}
add(5, 10);Here:
- a and b - parameters
- 5 and 10 - arguments
Output
15Order of Parameters and Arguments
Order matters in JavaScript
Example
function display(firstName, lastName) { console.log(firstName + " " + lastName);}
display("Rahul", "Sharma");Output
Rahul SharmaIf the order is changed
display("Sharma", "Rahul");Output
Sharma RahulJavaScript assigns arguments based on position, not name.
Missing Arguments in JavaScript
If fewer arguments are passed than parameters, JavaScript assigns undefined.
Example
function showAge(age) { console.log(age);}
showAge();Output
undefinedExtra Arguments in JavaScript
If more arguments are passed than parameters, JavaScript ignores the extra ones.
Example
function showName(name) { console.log(name);}
showName("Amit", 25, "India");Output
AmitDefault Parameters (Modern JavaScript)
JavaScript allows you to set default values for parameters.
Example
function greet(name = "Guest") { console.log("Hello " + name);}
greet();greet("Neha");Output
Hello GuestHello NehaDefault parameters prevent undefined values.
Why Parameters and Arguments Are Important
- Make functions reusable
- Allow dynamic input
- Reduce code duplication
- Improve readability and maintainability
Without parameters, functions would work only for fixed values.
Real World Use Case
function calculateTotal(price, quantity) { return price * quantity;}
let total = calculateTotal(500, 2);console.log(total);Output
1000This pattern is used everywhere: carts, forms, APIs, calculations.
Two Minute Drill
Parameters
Arguments
Parameters
- Defined in the function declaration
- Act as placeholders
- Receive values when the function is called
Arguments
- Passed during function invocation
- Are actual values
- Assigned based on position