Functions & reusable behavior
Understand the idea
A handler receives where it expected a result. The helper calculated the answer but never returned it.
Give a repeated job a name
If a checkout calculates the same fee in several places, copying that calculation makes it easy for one version to drift. A function gives the calculation one home. A good name tells the reader what job it performs; its inputs say what information it needs.
Parameters, arguments, and return
Give each function a small, understandable job. Its inputs are local names available during the call. Returning a result lets other code use the calculation without reaching into the function’s temporary .
A parameter is the name in the function definition; an argument is the actual value supplied in a call. return ends that call and hands back its result. Logging is different: it displays a message for you, but does not hand that message back as the function's result. With no return value, the caller receives .
| Form | When available | Behavior to remember |
|---|---|---|
| Function declaration | Usually throughout its enclosing scope | A named definition can be called before its written line |
| Function expression | After the assignment executes | The variable declaration controls early access |
| Arrow function | After the assignment executes | A concise expression body returns implicitly; braces need return |
| Default parameter | When its argument is missing or undefined | null is an explicit argument, not a missing one |
| Rest parameter | When the function is called | Collects remaining arguments into one array |
| IIFE | Immediately at its written call | Creates a local scope and runs once there |
Ways to write a function
A declaration gives a function a name directly and is generally available before its written line in the same . An creates a function value where that expression is evaluated, often assigning it to a variable. An arrow is another expression form: a single expression body returns its result, while a body with braces needs an explicit return. Arrows also inherit this from their surrounding code, which the this lesson explores.
Defaults and groups of inputs
A function can accept more than one input, and not every caller needs to supply all of them. Decide what an omitted input should mean before choosing defaults. Making that meaning explicit is easier to maintain than a collection of hidden assumptions about missing values.
Default parameters are used when an argument is omitted or explicitly undefined; passing does not use the default. A rest parameter, written with three dots before its name, collects remaining arguments into an . It must be the last parameter. These tools keep callers simple without making the function guess what an input means.
When and where the work runs
Because functions are values, another function can receive one and choose when to call it. That received function is a . Pass the function value when the other code should control timing; adding parentheses immediately calls it and passes its result instead.
An immediately invoked function expression, or IIFE, is a function expression followed by a call right away. You may see it creating a private in older browser code. Modern usually provide that boundary for a whole file. In the example, follow checkout into addFee and back again: the returned number becomes the input to console.log.
Mental model
EXECUTING CODE
function addFee(price) { // Define the calculation and name its input.return price + 1; // Give the calculated amount back to the caller.
}
function checkout() { // Group the checkout work.console.log(addFee(5)); // Pass 5 in, then log the returned 6.
}
checkout(); // Start the checkout procedure.
CALL STACK / LAST IN, FIRST OUT
01 / 11Start script
Live example
checkout calls addFee with 5. addFee returns 6 to the waiting caller, which then logs it. The animation follows this same code.
Your console output appears here.
Guided challenge
Return the calculated total
Return the price with its fee added so checkout prints 6.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
6
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.