JavaScript(JS) JS set a default parameter value for a function

You can set default parameter values for a function in JavaScript using the assignment operator =. Here's an example:

function greet(name = "World") {
  console.log(`Hello, ${name}!`);
}

greet(); // Output: "Hello, World!"
greet("Alice"); // Output: "Hello, Alice!"
So‮ecru‬:www.theitroad.com

In this code, we define a function greet() that takes a name parameter. We set a default value of "World" for the name parameter by using the assignment operator = and providing the default value after the parameter name.

When the greet() function is called without any arguments, the default value of "World" is used for the name parameter. When the function is called with an argument (such as "Alice"), that value is used instead.

When we call the greet() function with and without an argument, we print a greeting to the console using console.log(). The output should show that the default value is used when no argument is provided, and the provided argument is used when one is provided.