0% found this document useful (0 votes)
6 views7 pages

Understanding JavaScript Function Expressions

A function expression in JavaScript is a function assigned to a variable, allowing it to be used as a callable entity. Unlike function declarations, function expressions are not hoisted and can be anonymous or named. They are versatile and commonly used for callbacks, closures, and other functional programming patterns.

Uploaded by

npradhan2503
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Understanding JavaScript Function Expressions

A function expression in JavaScript is a function assigned to a variable, allowing it to be used as a callable entity. Unlike function declarations, function expressions are not hoisted and can be anonymous or named. They are versatile and commonly used for callbacks, closures, and other functional programming patterns.

Uploaded by

npradhan2503
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1/16/26, 5:43 PM JavaScript Function Expressions

 Tutorials  References  Exercises  Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

JavaScript Function Expressions


❮ Previous Next ❯

What is a Function Expression?


A function expression is a function assigned to a variable.

A function expression is a way of defining a function within an expression, rather than as a


standalone declaration.

A function expression can be assigned to a variable, passed as an argument to another


function, or returned from a function.

Example

const x = function (a, b) {return a * b};

Try it Yourself »

[Link] 1/7
1/16/26, 5:43 PM JavaScript Function Expressions

After a function expression has been stored in a variable, the variable can be used as a
 function:Tutorials  References  Exercises  Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

Example

const x = function (a, b) {return a * b};

let z = x(4, 3);

Try it Yourself »

The function above is actually an anonymous function (a function without a name).

Functions stored in variables do not need function names. They are always invoked (called)
using the variable name.

Note
The functions above end with a semicolon because they are a part of an executable
statement.

Later in this Tutorial


Function expressions offer high flexibility and are widely used in JavaScript for various
purposes, and you will see a lot more of function expressions in the following chapters:

Callbacks:
Passing functions as arguments to other functions, such as event listeners or
asynchronous operations.

Closures:
Function expressions help create closures, which allow functions to remember and
access variables from their containing scope.

Arrow Functions:
The concise arrow function syntax (=>) is a modern way of writing function
expressions.
[Link] 2/7
1/16/26, 5:43 PM JavaScript Function Expressions

Immediately Invoked Function Expressions (IIFEs):


 Tutorialsthat
Functions  runReferences
as soon as theyExercises 
are defined, Sign In
often used to create private scopes
and avoid global variable conflicts.
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

Function vs. Function Expression


In JavaScript, function and function expression refer to different ways of defining
functions, their different syntax and how they are handled.

Function Declarations
A function declaration (or simply a function) is defined using the function keyword followed
by a function name, parameters, and a code block:

function add(a, b) {return a + b;}

Function declarations are "hoisted" to the top of their scope. This means you can call a
function before it is defined in the code:

let sum = add(2, 3);

function add(a, b) {return a + b;}

Note
Semicolons are used to separate executable JavaScript statements.

Since a function declaration is not an executable statement, it is not common to end it with
a semicolon.

[Link] 3/7
1/16/26, 5:43 PM JavaScript Function Expressions

Function
 Tutorials  Expressions
References  Exercises  Sign In

HTML CSS expression


A function JAVASCRIPT SQL
is defined PYTHON
by assigning JAVA
an anonymous PHP
function HOW TO [Link]
to a variable: C

const add = function(a, b) {return a + b;};

Note
Function expressions are commonly used to create anonymous functions.

Anonymous functions are functions without a name.

A function expressions can also be a named function assigned to a variable:

const add = function add(a, b) {return a + b;};

Function expressions are not hoisted in the same way as function declarations. They are
created when the execution reaches their definition, and cannot be called before that point:

let sum = add(2, 3); // ⛔ Will generate error.

const add = function (a, b) {return a + b;};

Key Differences
Syntax: Function declarations require a name. Function expressions can be
anonymous.
Hoisting: Function declarations are hoisted. Function expressions are not.
Flexibility: Function declarations offer more flexibility in how and where they are used.

[Link] 4/7
1/16/26, 5:43 PM JavaScript Function Expressions


Function Declaration
Tutorials  References  Function
Exercises 
Expression Sign In

Syntax: A statement starting with the Syntax: A part of an expression or assignment,


HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
function keyword, followed by a required and can be anonymous (without a name).
name.

Hoisting: The function is moved to the Hoisting: The variable holding the function
top of its scope before code execution, might be hoisted (depending on var, let, or
allowing you to call it from anywhere const), but the function assignment itself is not.
within that code scope, even before its You can only call it after it has been defined in
definition. the code scope.

Flexibility: Primarily used for general- Flexibility: Can be used in various contexts,
purpose functions that need to be such as arguments to other functions
available globally or in a specific scope. (callbacks), immediately invoked function
expressions, and assigned to object properties.

❮ Previous Sign in to track progress Next ❯

59

COLOR PICKER

[Link] 5/7
1/16/26, 5:43 PM JavaScript Function Expressions

 
 Tutorials  References  Exercises  Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

-->
 PLUS SPACES

GET CERTIFIED FOR TEACHERS

BOOTCAMPS CONTACT US

Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
AngularJS Reference
jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
[Link] 6/7
1/16/26, 5:43 PM JavaScript Function Expressions
SQL Examples SQL Certificate

 Tutorials  Python Examples


References 
[Link] Examples
Exercises  Python Certificate
PHP Certificate
Sign In
Bootstrap Examples jQuery Certificate
HTML
 CSS PHP Examples SQL
JAVASCRIPT PYTHON JAVA PHP JavaHOW
Certificate
TO [Link] C
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full
correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies
and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].

[Link] 7/7

You might also like