Open In App

What is the Syntax for Declaring Functions in JavaScript ?

Generally, in JavaScript, the function keyword is used to declare a variable. But, there are some more ways available in JavaScript that can be used to declare functions as explained below:

Example: The below code declares functions in JavaScript using the above-discussed approaches.




// Using function keyword
function GFG(){console.log("GeeksforGeeks");}
GFG();
 
// Anonymous function
const anonyFunc = function(){console.log("GFG");}
anonyFunc();
 
// Arrow function
const arrowFunc = () => {console.log("JavaScript");}
arrowFunc();
 
// Function constructor
const product = Function('num1', 'num2', 'return num1*num2');
console.log(product(3, 2));

Output
GeeksforGeeks
GFG
JavaScript
6
Article Tags :