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.
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.
-
1
Bottle the spell:
function cheer() { ... }functiontells JavaScript "I'm bottling some steps."cheeris the bottle's label, and everything between the braces{ }is the spell inside. Nothing runs yet — it's just stored. -
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
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 thirdcheer();!
Name an action, then call it.
function cheer() {
say("You can code!");
}
cheer();
cheer();
Program result
- Run your code to see output.
Type real JavaScript, then run it.
You used JavaScript to name a reusable action and call it more than once.
Open Function Builder