How to Call a Function in JavaScript ?
Last Updated :
29 Jan, 2024
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.
Javascript
function sayHello() {
console.log( "Hello, World!" );
}
sayHello();
|
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")
.
Javascript
function greet(name) {
console.log( "Hello, " + name + "!" );
}
greet( "Alice" );
|
Share your thoughts in the comments
Please Login to comment...