JavaScript Default Parameters

Beginner
6 min read
Updated: Nov 2026
3 examples

Definition and Usage

Default parameters let you write a fallback value in the parameter list. If the caller omits the argument or passes undefined, the default is used.

JavaScript Syntax

JavaScript
function greet(name = "friend") {
  return "Hello, " + name + "!";
}

console.log(greet("Ada"));
console.log(greet());
console.log(greet(undefined));
Try it Editor

How It Works

  1. Provided value. "Ada" replaces the default.
  2. Omitted. No argument means the default "friend" is used.
  3. undefined. Explicit undefined also triggers the default (unlike null).

Examples

Example 1 — null Does Not Trigger Defaults

Only undefined (or a missing arg) uses the default.

JavaScript
function label(text = "n/a") {
  return String(text);
}
console.log(label(null));
console.log(label());
Try it Editor

How It Works

  1. null stays. Defaults ignore null.
  2. Missing uses default. No argument yields "n/a".

Example 2 — Default Expressions

Defaults can call functions or read earlier parameters.

JavaScript
function area(width, height = width) {
  return width * height;
}
console.log(area(4));
console.log(area(4, 2));
Try it Editor

How It Works

  1. Square. When height is omitted it copies width.
  2. Rectangle. Passing both uses 4×2.

Example 3 — Default Object Options

Combine a default object with destructuring.

JavaScript
function connect({ host = "localhost", port = 80 } = {}) {
  return host + ":" + port;
}
console.log(connect());
console.log(connect({ port: 3000 }));
Try it Editor

How It Works

  1. Empty call. The whole parameter defaults to {}, then fields get defaults.
  2. Override port. Only port changes.

Frequently Asked Questions

When the argument is missing or undefined.

No. null is a defined value, so the default is skipped.

Yes. Later defaults may reference earlier parameters in the same list.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful