You are currently viewing 3 Types of Default Parameter in JavaScript Function

3 Types of Default Parameter in JavaScript Function

JavaScript Function Parameters Default Value

We can set default values for one or more parameters by assigning a value to the parameter during the function declaration. The default values will be used if the corresponding arguments are not provided when the function is called. So, in this article, we will see how to set the default value to the function parameters in javascript.

What are default function parameters?

Default values allow you to specify a fallback value that will be used if the caller does not provide a value for that parameter. It is a way to handle missing or undefined arguments in a function call. Default parameter values are specified by assigning a value to the parameter in the function’s parameter list. If the caller argues for that parameter, it will take precedence over the default value. If the caller does not provide an argument or passes undefined, the default value will be used.

It provides flexibility and simplifies function calls by allowing you to handle missing or undefined arguments in a more controlled manner.

When to set default function parameters in js?

We can set the default function JavaScript in the following scenarios:

  • If a function parameter is not required for the function to work correctly and has a commonly used default value.
  • If a function expects an optional configuration object with various properties, you can set default values for those properties directly in the function’s parameter list.
  • You can assign default values based on other parameters or variables within the function’s scope. This enables you to create more dynamic functions that adjust their behavior depending on the provided arguments.
READ ALSO  How to Use JavaScript for in loop | Example

So, setting default function parameters allows callers to have more control over the behavior of your functions while providing sensible fallbacks when necessary.

Why should we set default function parameters?

  • If the caller does not provide a value or passes undefined for a parameter. By setting default values, you ensure that the function can still execute correctly even if some arguments are missing. This helps prevent errors and allows for more robust code.
  • This simplifies function calls by allowing callers to omit arguments for optional parameters.
  • Automatically used when the caller does not provide explicit values.
  • It can communicate to other developers what values are expected when certain arguments are not provided. This makes your code more self-documenting and helps prevent confusion or misuse.

How to set default function parameters?

To set default parameter values, you can assign them directly within the function parameter parentheses. Here’s the general syntax:

function functionName(param1 = defaultValue1, param2 = defaultValue2, ...) {
  // Function body
}

Let’s explore some examples to illustrate how default parameter values work:

Example 1: Single Parameter with Default Value

function Demo(name = 'Guest') {
  console.log(`Hello, ${name}!`);
}

Demo(); // Output: Hello, Guest!
Demo('John'); // Output: Hello, John!

In the above example, the ‘Demo()’ function has a single parameter name with a default value of ‘Guest’. If no argument is provided when invoking the function, it uses the default value. However, if an idea is passed, the provided value is used instead.

Example 2: Multiple Parameters with Default Values

function Bike(make = 'Unknown', model = 'Unknown', year = 'Unknown') {
  console.log(`Bike: ${make} ${model} (${year})`);
}

createCar(); // Output: Car: Unknown Unknown (Unknown)
createCar('Yamaha', 'Model 3', 2022); // Output: Bike: Tesla Model 3 (2022)

In this example, the ‘Bike()’ function has three parameters: make, model, and year. Each parameter is assigned a default value of ‘Unknown’. This allows flexibility when calling the function. Developers can provide any combination of arguments, and the function will output the appropriate values.

READ ALSO  4 Easy Ways to Convert Array to String with Commas in JS

Example 3: Handle Undefined or Falsy Values:

This method is helpful for handling missing arguments as well as undefined or invalid values.

function calculateArea(length = 0, width = 0) {
  const area = length * width;
  console.log(`Area: ${area}`);
}

calculateArea(5, 8); // Output: Area: 40
calculateArea(5); // Output: Area: 0 (width is assigned default value of 0)

In the calculateArea function, default parameter values of 0 are assigned to length and width. This ensures that if either of the parameters is missing, undefined, or falsy, the function will still work correctly by treating the missing value as 0.

Conclusion

So, you can use the following methods to set the default function parameter.

Leave a Reply