Open In App

Difference between function expression vs declaration in JavaScript

Last Updated : 22 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Function Declaration: A Function Declaration( or a Function Statement) defines a function with the specified parameters without requiring a variable assignment. They exist on their own, i.e, they are standalone constructs and cannot be nested within a non-function block. A function is declared using the function keyword.

  • Syntax:
function gfg(parameter1, parameter2) {
 //A set of statements
 }

Function Expression: A Function Expression works just like a function declaration or a function statement, the only difference is that a function name is NOT started in a function expression, that is, anonymous functions are created in function expressions. The function expressions run as soon as they are defined.

  • Syntax:
var gfg = function(parameter1, parameter2) {
 //A set of statements
 }

Example 1: Using a Function Declaration 

Output:

25

Example 2: Using a Function Expression 

Output:

25

Next Article

Similar Reads