Command Palette

Search for a command to run...

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

return ends the function, and no return means undefined

The last line logs the return value of console.log itself: it prints first, then returns undefined.

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?

return ends the function, and no return means undefined

Console

(nothing was printed)

Expected console output

negative zero positive
HI
shout returned: undefined
bare returned: undefined
printed first
undefined