Chapter VI · Functions

Default Parameters

Default parameters allow standard initialization values to be assigned if no argument or undefined is passed.

01 How It Works

Think of a default setting checkbox pre-selected on an options menu screen.

If an argument is omitted during invocation, the engine falls back to the preset default value expression.

example.js
function greet(name = "Guest") {
  console.log(`Hello, ${name}`);
}
greet(); // "Hello, Guest"
  1. 1. Define a parameter in the function signature.

  2. 2. Assign a default fallback value using the equals sign (=).

  3. 3. Invoke without arguments to trigger the fallback.

02 Practical Example

Here is how you might see this concept applied in real-world code:

practical.js
function calculateTotal(price, tax = 0.05) {
  return price + price * tax;
}

Key Takeaway

Default parameters prevent unexpected undefined errors when arguments are omitted.

Related Concepts