JavaScript AcademyProfessional Engineering
Level 1 · FoundationsModule 5: Functions and Reusable LogicLesson 22 of 150About 40 minutes

Lesson 22: Parameters and arguments

01Opening question

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.

You need: Lesson 21: Function declarations and expressions

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.

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.

Open the full glossary

06Visual learning object

Argument and parameter explorer. Type arguments for a call and see which parameter each one fills, including defaults and missing values.

Loading the interactive object…

Open the full Function argument explorer page with its guided experiment.

07Examples

ex-022-01One parameter, many calls Browser-executable JavaScript

Each call fills name with a different argument.

function welcome(name) {
  console.log(`Welcome back, ${name}`);
}
welcome("Ada");
welcome("Linus");
Sample input
None
Expected output
Welcome back, Ada Welcome back, Linus
Common mistake
Putting quotes around the parameter name in the template: "${'name'}".
Try this
Call welcome with your own name.
Source
examples/ex-022-01-one-parameter.js (repository pending publication)

ex-022-02Order matters Browser-executable JavaScript

Arguments fill parameters by position.

function lineTotal(quantity, unitPrice) {
  console.log(`${quantity} x $${unitPrice} = $${quantity * unitPrice}`);
}
lineTotal(3, 4);
lineTotal(4, 3);
Sample input
None
Expected output
3 x $4 = $12 4 x $3 = $12
Common mistake
Swapping arguments: the result matches here, but the label is wrong.
Try this
Add a third parameter for the item name.
Source
examples/ex-022-02-two-parameters.js (repository pending publication)

ex-022-03Default and missing values Browser-executable JavaScript

A default replaces undefined, and only undefined.

function label(name, currency = "USD") {
  console.log(name, currency);
}
label("Price");
label("Price", "EUR");
function noDefault(a, b) {
  console.log(a, b);
}
noDefault(1);
Sample input
None
Expected output
Price USD Price EUR 1 undefined
Common mistake
Expecting null to trigger a default; only undefined does.
Try this
Call label("Price", undefined) and predict.
Source
examples/ex-022-03-defaults.js (repository pending publication)

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.
Try this
Add a first parameter label before ...prices.
Source
examples/ex-022-04-rest-parameter.js (repository pending publication)

08Guided exercises

Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.

Fix the argument order

Match arguments to parameters by position.

function describe(product, price) {
  console.log(`${product} costs $${price}`);
}
describe(25, "Scarf");

Shipping label with a default

Use a default parameter.

function shippingLabel(name, country) {
  console.log(`${name}, ${country}`);
}
shippingLabel("Ada Lovelace", "UK");
shippingLabel("Grace Hopper");

Price with tax

Design a function with three parameters and a default.

// A shop needs one function that prints a line like:
// "Mug: 2 x $8.00 + tax = $17.60"

09Prediction

Commit to an answer first, then run the real code to compare.

function f(a, b = 2) {
  console.log(a + b);
}
f(1);
f(1, 5);
f(1, undefined);

What three numbers are printed?

10Debugging investigation

The discount argument is ignored

Calling finalPrice(80, 0.25) still charges the default 10% discount.

function finalPrice(price, rate = 0.1) {
  const discount = 0.1;
  const final = price * (1 - discount);
  console.log(`Final: $${final}`);
  return final;
}
const result = finalPrice(80, 0.25);

More cases in the Debugging Detective Laboratory

11Practical scenario

Personalised emails

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

Download the whole lesson folder (examples, exercises, solutions, tests) as ZIP

14Source files

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.
  • Rest parameters collect any number of arguments.

16AI learning prompts

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 explanation Parameters 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 prediction Argument 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.
Debugging Wrong 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 generation Function 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 review Long 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.