JavaScript Course
JavaScript
/
Beginner

Default Parameters

Definition

Allows named parameters to be initialized with default values if no value or `undefined` is passed to the function.

Explain Like I'm New

It's a fallback safety net. If you ask a user for their preferred theme, and they don't answer, you automatically set it to 'light' instead of breaking the app with 'undefined'.

Real World Example

Setting a default configuration object: `function initialize(config = { retries: 3 }) { ... }`

Common Use Cases

  • •Simplifying function signatures
  • •Avoiding `||` short-circuit hacks inside functions

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • How do you assign a default parameter?

intermediate

  • If you pass `null` as an argument, does the default parameter trigger?

advanced

  • Can a default parameter reference a previous parameter?

Flash Cards

Question

Does passing null trigger the default?

Click to reveal answer
Answer

NO! Default parameters ONLY trigger if the argument is strictly `undefined` (or omitted entirely). If you pass `null`, `false`, `0`, or `""`, the default parameter will NOT trigger.

Question

Can a default parameter reference a previous one?

Click to reveal answer
Answer

Yes! Parameters evaluate from left to right. You can do `function calc(price, tax = price * 0.1)`. The `tax` default evaluates using the `price` passed in.