26.
What is the purpose of using default parameters in JavaScript functions?
To limit the maximum number of arguments that can be passed to the function.
To prevent the function from being called without any arguments.
To set default values for the parameters in case no arguments are passed.
To specify the minimum number of arguments required for the function.
Correct: C
Default parameters in JavaScript functions allow us to set a default value for a parameter in case no argument is passed when calling the function. This provides a fallback value that is used when the parameter is not explicitly provided, ensuring that the function does not throw an error or produce unexpected results.
Example:
function greet(name = 'Guest', message = 'Hello') {
console.log(`${message}, ${name}!`);
}
// Calling the function without providing arguments
greet(); // Output: Hello, Guest!
// Calling the function with arguments
greet('Alice', 'Hi'); // Output: Hi, Alice!