To call a function in JavaScript, you need to follow these basic steps:
- Define the function with a name and any necessary parameters:
javascriptfunction functionName(parameter1, parameter2) {
// code to be executed
}
- Call the function by using its name and passing any required arguments:
javascriptfunctionName(argument1, argument2);
Here's an example that shows how to call a function named greet
that takes a parameter name
and displays a greeting message:
javascriptfunction greet(name) {
console.log(`Hello, ${name}!`);
}
greet('John'); // output: Hello, John!
In this example, we called the greet
function and passed the argument 'John'
to it. The function then executed the code inside its block and displayed the message "Hello, John!" in the console.