Back to Loop Challenge
Code Studio · Level 5

Real JavaScript Functions

Bottle a spell. A function wraps a set of steps into one named power you can call whenever you want — write it once, cast it forever.

15-20 min Ages 9-12 Real Code JavaScript

What is a function?

A function is like a bottled spell. You write the magic steps once, give the bottle a name, and cork it. After that, saying the name — cheer() — uncorks the bottle and the whole spell runs. Say it twice, it runs twice.

say("You can code!") cheer()
  1. 1

    Bottle the spell: function cheer() { ... }

    function tells JavaScript "I'm bottling some steps." cheer is the bottle's label, and everything between the braces { } is the spell inside. Nothing runs yet — it's just stored.

  2. 2

    Cast it by name: cheer();

    Writing the name with parentheses calls the function — uncork, run the spell, done. The example calls it twice, so the robot cheers twice. One bottle, endless casts.

  3. 3

    Why coders use them

    If a game says "play a sound, add a point, flash the screen" every time you score, that's one scorePoint() function called from everywhere. Write once, use forever — and fixing it in one place fixes it everywhere. Try adding a third cheer();!

Code Editor

Name an action, then call it.

Example code
function cheer() {
  say("You can code!");
}

cheer();
cheer();
Output

Program result

Robot ready to speak your strings

Type real JavaScript, then run it.