A greeting function always says "Hello, Ada". How can the same function greet any customer?
Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.
02Start with working code
name is a parameter: a placeholder filled by the argument in each call.
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet("Ada");
greet("Grace");
03Objective and prerequisites
Learning objective: Define parameters, pass arguments, use default parameters, handle missing arguments, and explain that primitives are passed by value.
Skills practised: Functions, Parameters and arguments.
04Explanation
Parameters are names in the function's brackets: function greet(name). They act like variables inside the function.
Arguments are the actual values you pass when calling: greet("Ada"). The first argument goes to the first parameter, the second to the second, and so on.
If you pass fewer arguments than parameters, the missing ones are undefined. A default parameter gives a fallback: function greet(name = "guest").
Extra arguments are ignored unless the function asks for them. Order matters: price(qty, unit) and price(unit, qty) give different meanings.
Primitives are passed by value: reassigning a parameter does not change the caller's variable. Objects are passed by sharing: the function receives a reference, so mutating the object is visible outside.
Defaults apply only when the argument is undefined, not when it is null or 0. Defaults are evaluated at call time and can refer to earlier parameters.
Rest parameters (...items) collect any number of arguments into a real array; prefer them over the legacy arguments object.
When a function needs more than three parameters, accept one options object with destructuring: createOrder({ id, qty, gift = false }). Calls become self-describing and order-independent.
05Definitions
Parameter
A named input listed in a function definition.
Argument
The actual value passed to a function when it is called.
Default parameter
A value used when an argument is missing (undefined).
Rest parameter
...name collects remaining arguments into an array.
Pass by value
The function receives a copy of a primitive value.
ex-022-04Any number of arguments Browser-executable JavaScript
...prices collects all arguments into an array.
function sum(...prices) {
let total = 0;
for (const p of prices) total += p;
return total;
}
console.log(sum(2, 3));
console.log(sum(1, 2, 3, 4));
console.log(sum());
Sample input
None
Expected output
5
10
0
Common mistake
Placing ...rest before other parameters, which is a syntax error.
A newsletter tool sends the same email to thousands of customers with their own name and plan. One function with parameters produces every version.
function emailLine(name, plan = "Free") {
return `Hi ${name}, you are on the ${plan} plan.`;
}
console.log(emailLine("Ada"));
12Knowledge check
1. What is printed?
function hi(name) { console.log("Hi " + name); }
hi();
Hi undefined
Hi
An error
Hi null
2. In greet("Ada"), what is "Ada"?
A parameter
An argument
A return value
A default
3. What is printed?
function f(x = 10) { console.log(x); }
f(null);
10
5
null
0
4. Why is the result 2 x $5 for 5 items at $2?
function price(qty, unit) { console.log(`${qty} x $${unit}`); }
price(2, 5);
The template is wrong
The arguments are swapped
Multiplication is wrong
A default is missing
5. What does ...items collect?
The first argument
All remaining arguments as an array
Only numbers
Nothing
6. A function needs six settings. Which call is clearest?
createUser("Ada", 36, true, false, "UK", 2)
createUser({ name: "Ada", age: 36, admin: true })
Six separate functions
Global variables
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Fix the argument order: Prints correctly
Shipping label with a default: Prints both labels; Uses a default parameter
Price with tax: Prints both lines; taxRate has a default
ex-022-01: output matches the documented expected output
ex-022-02: output matches the documented expected output
ex-022-03: output matches the documented expected output
ex-022-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-01-foundations/module-05-functions-and-reusable-logic/lesson-022-parameters-and-arguments/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
Parameters are placeholders; arguments are the values passed.
Arguments match parameters by position.
Missing arguments are undefined; defaults replace undefined only.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationParameters and arguments
Context: I know how to call functions.
Task: Explain parameters, arguments and defaults with a shipping label example.
Constraints: Beginner level.
Expected behaviour: I can tell parameters from arguments.
Testing: Ask me five quick questions.
Code predictionArgument drill
Context: I know parameters and defaults.
Task: Give me six calls with missing, extra and null arguments and ask what is printed.
Constraints: Wait for my answers.
Expected behaviour: Each has one answer.
Testing: Explain default rules for any I miss.
DebuggingWrong values
Context: My function shows the price where the name should be.
Task: Help me find the argument-order bug with questions.
Constraints: No direct answer.
Expected behaviour: I fix it myself.
Testing: Suggest an options-object version.
Exercise generationFunction inputs
Context: I finished the price with tax exercise.
Task: Create two functions to write with three parameters and one default.
Constraints: Fictional data, exact output.
Expected behaviour: Include a call that uses the default.
Testing: Include expected output.
Code reviewLong parameter lists
Context: My function has five parameters.
Task: Show me how to switch to an options object.
Constraints: Keep behaviour identical.
Expected behaviour: Calls are self-describing.
Testing: Show before and after calls.