Open In App

What is a Function in JavaScript ?

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.




// 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.
Article Tags :