What are best practices for using default function parameters?

Default function parameters are a powerful feature in JavaScript that allow developers to provide default values for function parameters when no values or undefined are passed. This enhances code readability and reduces the need for additional checks within functions.

default parameters, JavaScript, best practices, function parameters

Here's an example of how to use default function parameters:


function greet(name = 'Guest', greeting = 'Hello') {
    return `${greeting}, ${name}!`;
}

console.log(greet()); // Output: Hello, Guest!
console.log(greet('Alice')); // Output: Hello, Alice!
console.log(greet('Bob', 'Good morning')); // Output: Good morning, Bob!
        

default parameters JavaScript best practices function parameters