Optional Function Arguments
JavaScript allows you to define functions with optional arguments by assigning default values to the parameters.
function greet(name = 'Anonymous') {
console.log(`Hello, ${name}!`);
}
greet(); // Output: Hello, Anonymous!
greet('John'); // Output: Hello, John!
In the example above, the greet function has an optional name parameter that defaults to 'Anonymous' if no value is provided. This allows you to call the function without specifying an argument.
No comments:
Post a Comment