Skip to content
engineering workbench.by StudyPort Academy
Learning path
Foundations15 minLESSON 08 / 34

Functions & reusable behavior

01

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 .

Function forms and input conventions
FormWhen availableBehavior to remember
Function declarationUsually throughout its enclosing scopeA named definition can be called before its written line
Function expressionAfter the assignment executesThe variable declaration controls early access
Arrow functionAfter the assignment executesA concise expression body returns implicitly; braces need return
Default parameterWhen its argument is missing or undefinednull is an explicit argument, not a missing one
Rest parameterWhen the function is calledCollects remaining arguments into one array
IIFEImmediately at its written callCreates 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.

02

Mental model

EXECUTING CODE

1
function addFee(price) { // Define the calculation and name its input.
2
  return price + 1; // Give the calculated amount back to the caller.
3
}
4
function checkout() { // Group the checkout work.
5
  console.log(addFee(5)); // Pass 5 in, then log the returned 6.
6
}
7
checkout(); // Start the checkout procedure.

CALL STACK / LAST IN, FIRST OUT

script()
Console is empty

01 / 11Start script

03

Live example

checkout calls addFee with 5. addFee returns 6 to the waiting caller, which then logs it. The animation follows this same code.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Return the calculated total

Return the price with its fee added so checkout prints 6.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

6

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Which argument activates a default parameter?
    02What does a function with no executed return produce?

    Ready to call this one understood?

    Mark this lesson complete to earn 100 XP. You can always revisit it.

    Sign in to save progress
    Next lesson