The Wayback Machine - https://web.archive.org/web/20240709094222/https://www.geeksforgeeks.org/difference-between-function-declaration-and-function-expression-in-javascript/
Open In App

Difference between ‘function declaration’ and ‘function expression’ in JavaScript

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

Functions in JavaScript allow us to carry out some set of actions, important decisions, or calculations and even make our website more interactive. In this article, we will learn the difference between ‘function declaration’ and ‘function expression’. The similarity is both use the keyword function and the most prominent difference is that the function declaration has a function name while the latter doesn’t have one.

Function Declaration

  • A function declaration also known as a function statement declares a function with a function keyword. The function declaration must have a function name.
  • Function declaration does not require a variable assignment as they are standalone constructs and they cannot be nested inside a functional block.
  • These are executed before any other code.
  • The function in the function declaration can be accessed before and after the function definition.

Syntax:

function geeksforGeeks(paramA, paramB) {
// Set of statements
}

Example: The following example illustrates a function declaration where we do the addition of two numbers.

Javascript




// Function Declaration
function geeksforGeeks(paramA, paramB) {
    return paramA + paramB;
}
 
let result = geeksforGeeks(5, 5);
console.log('Sum=', result);


Output

Sum= 10

Function Expression

  • A function Expression is similar to a function declaration without the function name.
  • Function expressions can be stored in a variable assignment.
  • Function expressions load and execute only when the program interpreter reaches the line of code.
  • The function in the function expression can be accessed only after the function definition.

Syntax:

let geeksforGeeks= function(paramA, paramB) {  // Set of statements}

Example: The following example illustrates a function expression where we do the addition of two numbers.

Javascript




let geeksforGeeks = function (paramA, paramB) {
    return paramA + paramB;
}
 
let result = geeksforGeeks(5, 5);
console.log("Sum: ",result);


Output

Sum:  10

Difference between Function Declaration and Function Expression

Function Declaration Function Expression
A function declaration must have a function name. A function expression is similar to a function declaration without the function name.
Function declaration does not require a variable assignment.  Function expressions can be stored in a variable assignment.
These are executed before any other code. Function expressions load and execute only when the program interpreter reaches the line of code.
The function in function declaration can be accessed before and after the function definition. The function in function expression can be accessed only after the function definition.
Function declarations are hoisted Function expressions are not hoisted
Syntax: function geeksforGeeks(paramA, paramB) { // Set of statements } Syntax: var geeksforGeeks= function(paramA, paramB) { // Set of statements }


Similar Reads

Difference between function expression vs declaration in JavaScript
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(paramet
1 min read
Difference between AngularJS Expression and Angular Expression
AngularJS is a JavaScript-based framework that can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. Angular on the other hand is a client-side TypeScript-based, front-end web framework by Google. Angular is
3 min read
Using the function* Declaration in JavaScript
The function* declaration is used to define a generator that returns a Generator object. Generators are very powerful for asynchronous programming as they aim to resolve callback problems. In a generator, the yield keyword is used instead of return. The yield statement suspends the function’s execution and sends a value back to the caller, but reta
2 min read
JavaScript ReferenceError - Can't access lexical declaration`variable' before initialization
This JavaScript exception can't access the lexical declaration `variable' before initialization occurs if a lexical variable has been accessed before initialization. This could happen inside any block statement when let or const declarations are accessed when they are undefined. Message: ReferenceError: Use before declaration (Edge) ReferenceError:
2 min read
JavaScript SyntaxError - Missing = in const declaration
This JavaScript exception missing = in const declaration occurs if a const is declared and value is not provided(like const ABC_DEF;). Need to provide the value in same statement (const ABC_DEF = '#ee0'). Message: SyntaxError: Const must be initialized (Edge) SyntaxError: missing = in const declaration (Firefox) SyntaxError: Missing initializer in
1 min read
JavaScript SyntaxError - A declaration in the head of a for-of loop can't have an initializer
This JavaScript exception a declaration in the head of a for-of loop can't have an initializer occurs if the for -of loop contains an initialization expression like |for (var i = 0 of iterable)|. This is not valid initialization in for-of loops. Message: SyntaxError: for-of loop head declarations cannot have an initializer (Edge) SyntaxError: a dec
1 min read
How to define all the list properties in one declaration using CSS ?
Sometimes a web page has good content for reading but the styling of the texts looks improper, so it becomes boring for the readers, and lastly, they leave the web page. But when they read articles with proper styling and lists they read them fully because of the good visuals stated there which keep them attracted to the article and readings. So wh
3 min read
CSS Style Declaration removeProperty() Method
The removeProperty() method is used to remove the existing CSS property. Syntax: It is used to remove the specified CSS property. object.removeProperty(propertyname)Parameters: It accepts a single parameter: propertyname: It is a required parameter that contains a string that represents the name of the property to be removed.Return value: It return
1 min read
SASS | Property Declaration
Property Declarations in SASS are used to define how the elements matching a selector need to be styled. Along with that SASS adds some extra features to property declarations in order to make them easier to write and to automate them. Firstly, a declaration value can be any expression which can be evaluated and included in the result. Example: .re
2 min read
How to set different background properties in one declaration?
We are going to use the Shorthand property in this article to set different background properties. To reduce the length of the CSS code we can declare background properties in one line through "Shorthand Property".Through this property we can define different properties of background in a single line instead of using background-color, background-im
2 min read
three90RightbarBannerImg