30 min·beginner

Lesson 5

Parameters & returns

Build a practical mental model for parameters & returns and use it in working JavaScript.

By the end, you will be able to…

  • Explain parameters & returns in your own words
  • Use parameters & returns in a small program
  • Recognize and debug a common failure mode

Build the mental model

Default parameters supply a fallback when an argument is omitted, and every meaningful function should return a value the caller can use rather than only logging internally.

Syntax and working example

Read the code from inputs to output. Predict what it prints, then run it and change one value.

Working example
Expected output: USD 9.50

Why this matters

Functions that return values compose — you can pass one function's output straight into another — while functions that only log cannot.

Real-world example

Utility libraries like date and currency formatters are built entirely from small functions that take inputs and return a usable result.

Common mistakes

  • Logging inside a function instead of returning, making the result unusable elsewhere
  • Forgetting a default value and getting undefined for optional arguments
  • Mutating a parameter instead of returning a new value

Try it yourself

Challenge

Add a default 'decimals' parameter that controls how many digits formatPrice shows.

Practice

Knowledge check

Why should most functions return a value instead of only calling console.log?

Summary

Give parameters sensible defaults and always return a usable value instead of only logging.