JavaScript Optional Parameters and Default Values

JavaScript optional parameters: default parameters, undefined vs null, options objects, destructuring defaults, and arguments.length patterns.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

JavaScript Optional Parameters and Default Values

Optional parameters let callers omit arguments while the function still runs with sensible behavior. ES2015 default parameters (function f(x = 1)) evaluate the initializer when the argument is missing or explicitly undefined; an options object scales better when a function exposes many toggles. Destructuring defaults (function init({ port = 3000 } = {})) build on the same idea—see JavaScript destructuring when that pattern is new to you.

Environment: Node.js v20.18.2. After each runnable snippet, the following paragraph states the expected console output (order and values).


Method 1: Use Default Parameter Values

javascript
function totalWithTax(amount, rate = 0.1) {
  return amount + amount * rate;
}

console.log(totalWithTax(100));
Output

You should see one line logging 110.

When rate is omitted, JavaScript uses the default value 0.1.


Method 2: Understand undefined and null

Default parameters are used when an argument is omitted or explicitly undefined.

javascript
function totalWithTax(amount, rate = 0.1) {
  return amount + amount * rate;
}

console.log(totalWithTax(100, undefined));
console.log(totalWithTax(100, null));
Output

You should see 2 lines, in order: 110, 100.

null is a real value, so it does not trigger the default. In multiplication, null behaves like 0.


Method 3: Use an Options Object for Many Optional Parameters

javascript
function greet(name, { title = "", punctuation = "!" } = {}) {
  return `${title}${name}${punctuation}`;
}

console.log(greet("Ana", { title: "Dr. ", punctuation: "." }));
Output

You should see one line logging Dr. Ana..

An options object is easier to read than many positional optional parameters.


Method 4: Check arguments.length When Needed

Default parameters usually remove the need for arguments.length, but it can still tell whether a caller passed an argument.

javascript
function wasProvided(value) {
  return arguments.length > 0;
}
Output

Use this only when you must distinguish omitted arguments from explicit undefined.


Common Questions About JavaScript Optional Parameters

Does JavaScript support optional parameters?

Yes. JavaScript supports default parameter values, and callers can omit arguments.

What is the best pattern for many optional parameters?

Use an options object with destructuring defaults.

Does null trigger a default parameter?

No. Only omitted arguments and undefined trigger default parameter values.


Summary

JavaScript optional parameters are best handled with default parameter values for simple cases and options objects for functions with several optional settings. Defaults apply when an argument is omitted or undefined, but not when it is null. This makes function APIs clearer and avoids fragile positional argument lists.


Official Documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …