A function behind a button
area computes and returns; showArea does a job on the page and returns nothing. Both are normal functions.
A function is a named block of code you define once and run (call) as often as you like; it receives inputs through parameters, and sends a result back with return, or undefined when there is no return.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write a function square(n) that returns n * n, and print square(7).
Write isEven(n) that returns true for even numbers and false otherwise, and test it with 4 and 7.
Write grade(score) that returns "A" for 90 and above, "B" for 75 and above, and "C" otherwise, using early returns (no else).
Explain it without notes
What is the difference between a parameter and an argument?
What does return do, and what does a function return when it has no return statement?
What is the difference between greet and greet()?
What happens if you call a function with fewer or more arguments than it has parameters?
Preview
Console
Nothing logged yet.