← Back to Roadmap

Default Parameters

Easy

In plain terms

Assign default values in the parameter list: function fn(a = 1, b = 2). Defaults apply when undefined is passed or the argument is omitted. Can use expressions and previous parameters.

What you need to know

  • param = defaultValue
  • Used when undefined or omitted
  • Can reference earlier params

Try it yourself

Copy the code below and run it in your browser console or a code editor:

function greet(name = 'Guest') {
  return 'Hello, ' + name;
}
greet();        // "Hello, Guest"
greet('Alice'); // "Hello, Alice"

function multiply(a, b = a) {
  return a * b;  // b defaults to a
}
multiply(5);  // 25

function createUser(name, role = 'user') {
  return { name, role };
}

Learn more

Dive deeper with these trusted resources: