Open In App

How to Call a Function in JavaScript ?

Calling a function in JavaScript is quite straightforward. You use the function name followed by parentheses ().

Example 1: Here, sayHello is the function, and to call it, you simply write sayHello(). If the function takes parameters, you include them inside the parentheses.






function sayHello() {
    console.log("Hello, World!");
}
 
// Calling the function
sayHello();

Output
Hello, World!

Example 2: Here, the function greet takes a name parameter, and when calling it, you provide a value for the parameter like this: greet("Alice").






function greet(name) {
    console.log("Hello, " + name + "!");
}
 
// Calling the function with an argument
greet("Alice");

Output
Hello, Alice!
Article Tags :