Optional Function Arguments

 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

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...