Sikau

JavaScript · Day 23 · about 25 min

What a function is

A recipe card you can use again.

Write once, use often

Your family's chiya recipe lives on a card. Whenever someone wants tea, they don't reinvent it — they follow the card. A function is a recipe card for code: write the steps once, then use them by name whenever you like.

Defining and calling

function makeChiya() {
  console.log("Boil water and milk");
  console.log("Add tea leaves and spices");
  console.log("Strain and serve ☕");
}

makeChiya();
makeChiya();

Name functions with a verb: makeChiya, showMenu, greetUser. They do things.

The rest of this lesson

  • Why programmers love functionsexplain
  • Predict the outputpredict
  • Defined but called?predict
  • Define and callfill
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 68 lessons of JavaScript