Default Parameters in JavaScript

Javascript 7 min min read Updated: Mar 09, 2026 Intermediate
Default Parameters in JavaScript
Intermediate Topic 5 of 15

Default parameters are a feature introduced in ES6 that allows developers to assign default values to function parameters. If a value is not provided when the function is called, JavaScript automatically uses the default value.

Before ES6, developers had to manually check whether parameters were provided and assign values if they were missing. Default parameters simplify this process and make code easier to read and maintain.

What are Default Parameters?

Default parameters allow functions to have predefined values for parameters. If no argument is passed to the function, the default value is used automatically.

Key Point: Default parameters ensure that functions work correctly even when arguments are missing.

Example Without Default Parameters

Before ES6, developers had to manually check whether parameters were undefined.

javascript function greet(name){ if(name === undefined){ name = "Student"; } console.log("Hello " + name); } greet();
Output

Hello Student

Using Default Parameters

With ES6, developers can assign default values directly in the function definition.

javascript function greet(name = "Student"){ console.log("Hello " + name); } greet();
Output

Hello Student

Passing Custom Values

If a value is passed when calling the function, it overrides the default value.

javascript function greet(name = "Student"){ console.log("Hello " + name); } greet("Rahul");
Output

Hello Rahul

Key Point: Default parameters are used only when no argument is provided.

Multiple Default Parameters

Functions can also have multiple parameters with default values.

javascript function add(a = 0, b = 0){ console.log(a + b); } add();
Output

0

Using Expressions as Default Values

Default parameter values can also be expressions or calculations.

javascript function multiply(a, b = a * 2){ console.log(b); } multiply(5);
Output

10

Benefits of Default Parameters

  • Reduces the need for manual parameter checks
  • Makes functions easier to read
  • Prevents errors caused by missing arguments
  • Simplifies function logic

Conclusion

Default parameters make JavaScript functions more flexible and reliable by providing fallback values when arguments are missing. This ES6 feature helps developers write cleaner and more maintainable code.

Default parameters are widely used in modern JavaScript development and are supported by all modern browsers.

In the next tutorial, you will learn about Rest and Spread Operators in JavaScript, which provide powerful ways to handle function arguments and arrays.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators