Is having the first JavaScript parameter with default value possible?

In JavaScript, you can have default values for the first parameter, but you need special techniques to skip it when calling the function. The most effective approach is using destructuring with spread syntax.

The Challenge

When you define a function with a default value for the first parameter, you cannot simply omit it in a regular function call:

function multiply(firstParam = 10, secondParam) {
    return firstParam * secondParam;
}

// This won't work as expected:
multiply(5); // firstParam = 5, secondParam = undefined

Solution: Using Destructuring with Spread Syntax

You can skip the first parameter by using destructuring with an empty slot:

function multiply(firstParameterDefaultValue = 10, secondParameterValue) {
    return firstParameterDefaultValue * secondParameterValue;
}

// Skip first parameter using destructuring
console.log("The result = " + multiply(...[, 10]));
The result = 100

Alternative Approach: Using undefined

You can also explicitly pass undefined to trigger the default value:

function multiply(firstParam = 10, secondParam) {
    return firstParam * secondParam;
}

console.log("Using undefined:", multiply(undefined, 15));
console.log("Normal call:", multiply(5, 3));
Using undefined: 150
Normal call: 15

Comparison of Methods

Method Syntax Readability
Destructuring multiply(...[, 10]) Less clear
Undefined multiply(undefined, 10) More explicit

Best Practice: Consider Function Design

Instead of having the first parameter with a default, consider reordering parameters or using an options object:

// Better design: required parameters first
function multiply(secondParam, firstParam = 10) {
    return firstParam * secondParam;
}

console.log("Better approach:", multiply(5)); // Uses default for second parameter
Better approach: 50

Conclusion

While it's possible to have default values for the first parameter using destructuring or undefined, it's better to design functions with required parameters first. This makes your code more intuitive and maintainable.

Updated on: 2026-03-15T23:18:59+05:30

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements