Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 3.1 — Declaring and Calling Functions
JavaScript · Example 1 of 3

Define once, call many times

greet(...) runs the function; greet alone is the function as a value. length is how many parameters it declares.

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.

01

Write a function square(n) that returns n * n, and print square(7).

02

Write isEven(n) that returns true for even numbers and false otherwise, and test it with 4 and 7.

03

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

01

What is the difference between a parameter and an argument?

02

What does return do, and what does a function return when it has no return statement?

03

What is the difference between greet and greet()?

04

What happens if you call a function with fewer or more arguments than it has parameters?

Define once, call many times

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

Hello, Ravi!
Hello, undefined!
Hello, Mei!
function
[Function: greet]
greet 1