GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • hoisted
  • callbacks

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — JavaScript Function Expression - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

JavaScript Function Expression

Last Updated : 23 Jan, 2026

A function expression is a way to define a function by assigning it to a variable or using it within an expression, allowing the function to be stored, passed as an argument, or executed immediately.

  • Function expressions can be named or anonymous.
  • They are not hoisted, meaning they are accessible only after their definition.
  • Frequently used in callbacks, closures, and dynamic function creation.
  • Enable encapsulation of functionality within a limited scope.
JavaScript
const greet = function(name) {
    return `Hello, ${name}!`;
};
console.log(greet("Steven"));
  • The function(name) creates an anonymous function assigned to the greet variable.
  • The function takes name as a parameter and returns a greeting string.
  • Calling greet("Ananya") invokes the function and outputs the greeting.

Syntax

const fName = function(params) {
// function body
};
  • fName: Variable storing the function.
  • function(params): Defines the function. Parameters are optional.
  • { // function body }: Contains the logic to execute when the function is called.

A function expression is a way to define a function as part of an expression making it versatile for assigning to variables, passing as arguments, or invoking immediately.

Named vs Anonymous Function Expressions

Anonymous Function Expression: The function has no name and is typically assigned to a variable.

JavaScript
const sum = function(a, b) {
    return a + b;
};
console.log(sum(5, 3));

Named Function Expression: The function is given a name, which is useful for recursion or debugging.

JavaScript
const factorial = function fact(n) {
    if (n === 0) return 1;
    return n * fact(n - 1);
};
console.log(factorial(5));

Use Cases of Function Expressions

1. Storing in Variables

Function expressions can be stored in variables so they can be reused easily whenever needed.

JavaScript
const add = function(x, y) {
    return x + y;
};
console.log(add(3, 5)); 

2. Callback Functions

Callback functions are functions passed as arguments to other functions and are executed later when needed.

JavaScript
setTimeout(function() {
    console.log("This message appears after 3 seconds!");
}, 3000);

3. Event Handlers

Function expressions are useful for event listeners because they allow you to define a function directly where it is needed.

JavaScript
document.querySelector("button").addEventListener("click", function() {
    console.log("Button clicked!");
});

4. Self-Invoking Functions

Function expressions can be immediately executed.

JavaScript
(function() {
    console.log("This is a self-invoking function!");
})();

Advantages of Function Expressions

  • Flexibility: Can be used as callbacks, event handlers, or part of expressions.
  • Encapsulation: Keeps the scope limited and avoids polluting the global namespace.
  • Control Over Execution: Executes only when explicitly invoked.

Arrow Functions: A Variant of Function Expressions

Arrow functions are a concise and modern way to define function expressions. They are particularly useful for short, single-purpose functions.

JavaScript
const arrowFunc = (param1, param2) => param1 + param2;
console.log(arrowFunc(5, 7)); 

Features:

  • Implicit return for single-line functions.
  • No binding of this, making them unsuitable for methods requiring a this context.

Difference Between Function Expression and Function Declaration

Here are some difference between Function expression and Function Declaration:

Function ExpressionFunction Declaration
Function Expression cannot be hoisted, so they cannot be called before their definition.Function Declaration are Hoisted so that can be called before their definition.

Can be passed as arguments or stored in variables

Typically used as standalone functions
Defined within an expression.Uses function keyword with a name.
Useful for callbacks and dynamic functions.Best for defining reusable functions.

Can be assigned to variables or passed as arguments

Typically used as standalone functions

Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.