Solice UI
Back to the curriculum

Chapter 4 · JavaScript & the DOM

Functions and scope

A function gives a piece of behavior a name and a boundary. It can receive input, produce a result, and hide temporary details that other code does not need to manage.

Beginner · Lesson 3 of 6 in this chapter

Parameters make behavior reusable

A parameter names an input the caller provides. Returning a value lets the caller decide what to do with the result instead of forcing every function to change the page directly.

Scope limits where names exist

Variables declared inside a function or block are normally available only there. Smaller scopes reduce accidental interference and make it easier to see which code can change a value.

Formatting a result count

  1. A function receives the numeric count.
  2. It chooses singular or plural wording.
  3. It returns the finished sentence for the page to display.
  • Functions name and isolate behavior.
  • Parameters are inputs; return values are outputs.
  • Keep values in the smallest useful scope.