Open In App

What is a Function in JavaScript ?

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A function is a small block of code that will execute only when it is called or invoked in the code. The functions enhance the reusability of our code by preventing us from writing the same code again and again. We can simply call a function again in the code where we need a similar kind of code that is written in the body of a function. The execution of the code written in a function depends on us, as it will be executed only if it is called. Otherwise, it will not execute.

Example: The below code will explain how you can create and invoke a function in JavaScript.

Javascript




// Function declaration
function GFG(){
    console.log("GeeksforGeeks is a Computer Science Portal.");
}
 
// Invoking a Function
// It will not execute without calling it.
GFG();


Output

GeeksforGeeks is a Computer Science Portal.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads