codequick-darkmode-logo
LoginSign Up
  • javascript-advance

Default Parameters in ES6

ES6 (ECMAScript 2015) introduced several new features to JavaScript, and one of the notable additions is the support for default function parameters. Default parameters allow you to specify default values for function arguments when they are not explicitly provided.

In JavaScript, when a function is invoked, it is possible to pass arguments to the function. However, if an argument is not provided, JavaScript assigns the value undefined to that argument. This can sometimes lead to unexpected behavior if your function relies on specific values for its arguments.

With default parameters in ES6, you have the ability to define default values for these arguments, ensuring that your functions behave as expected even when certain arguments are not passed. Let's take a closer look at how default parameters work in ES6.

Basic Syntax

To define a default parameter for a function, you can assign a value directly to the parameter in the function's declaration. Here's an example:

function greet(name = 'Anonymous') { console.log('Hello, ' + name + '!'); } greet('John'); // Output: Hello, John! greet(); // Output: Hello, Anonymous!

In this example, the greet function has a single parameter named name. We have assigned the default value of 'Anonymous' to the name parameter by using the assignment operator (=).

When the greet function is invoked without any arguments, the default parameter 'Anonymous' is used, and the output is 'Hello, Anonymous!'. However, if we pass an argument, such as 'John', the value of name becomes 'John', and the output is 'Hello, John!'.

Applying Default Parameters

Default parameters can be applied to any parameter in a function. They are especially useful when you have functions with multiple arguments and want to provide fallback values for some of them.

function calculatePrice(quantity, pricePerUnit = 10) { return quantity * pricePerUnit; } console.log(calculatePrice(5)); // Output: 50 console.log(calculatePrice(3, 15)); // Output: 45

In this example, the calculatePrice function takes two parameters: quantity and pricePerUnit. The pricePerUnit parameter has a default value of 10. If the pricePerUnit argument is not passed, the function uses the default value of 10 instead. When we invoke the calculatePrice function with quantity = 5, it uses the default pricePerUnit of 10 and returns 50. When we pass quantity = 3 and pricePerUnit = 15, the function calculates and returns 45.

Default Parameter Expressions

Default parameter values can also be expressions or function calls. This means that you can use functions and operations to compute default values dynamically.

function greet(name = getName()) { console.log('Hello, ' + name + '!'); } function getName() { return 'Anonymous'; } greet(); // Output: Hello, Anonymous!

In this example, the default value for the name parameter is the result of calling the getName function. When the greet function is invoked without an argument, it uses the default value returned by getName(), resulting in the output 'Hello, Anonymous!'. This demonstrates the flexibility of default parameter expressions in JavaScript.

Undefined vs. Default Parameters

One important thing to note is that providing undefined explicitly as an argument still uses the default parameter value. Let's look at an example to understand this:

function greet(name = 'Anonymous') { console.log('Hello, ' + name + '!'); } greet(undefined); // Output: Hello, Anonymous!

In this case, even though we pass undefined as the argument to the greet function, it still uses the default parameter value 'Anonymous'. This can be useful in situations where you want to explicitly define some arguments and allow others to fall back to their default values.

Conclusion

Default parameters in ES6 allow you to provide fallback values for function arguments. They ensure that your functions behave as expected even when certain arguments are not passed. With the ability to define default values directly in the function's declaration, you can easily specify default parameters that make your code more robust and maintainable.

If you want to learn more about default parameters and other ES6 features, make sure to check out the MDN web docs on default parameters and the W3Schools guide to ES6.