LESSON 30 OF 50

Use a default parameter

Provide a fallback when the call supplies no argument.

Official sourceECMAScript — function parameter initializersReviewed: 2026-08-26
STEP 1 · LEARN

How it works

A parameter initializer is evaluated when the corresponding argument is missing or undefined. This gives the function a clear fallback without repeated checks in its body.

Correct example
function greet(name = 'vizitator') {
  console.log(`Salut, ${name}!`);
}

greet();

The call without an argument uses the “vizitator” default.

Common mistake
function greet(name) { console.log(`Salut, ${name}!`); }
greet();

Without an initializer, name becomes undefined and the message is not the intended one.

STEP 2 · APPLY

Your exercise

Declare greet with the default name “vizitator”, build the message and call the function without an argument.

STEP 3 · RUN

Write and run the code

script.jsJAVASCRIPT
Isolated consoleisolated
Preparing the runtime…
Run the code to see the result.
The draft stays only in this browser.Isolated QuickJS · no DOM, session or network
STEP 4 · CHECK

What needs to pass

0%not checked
  • greet declares name with the vizitator default and is called without an argument.
  • The message includes name in a template literal.
  • The output is Salut, vizitator!.
Explain the solution logic

The parameter initializer supplies the value before the body builds the message.