Open In App

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

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 }


Last Updated : 03 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads