codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

15 / 65 topics
14JavaScript Functions15JavaScript Function Expressions16JavaScript Arrow Functions17JavaScript Variable Scope18JavaScript Hoisting19JavaScript Closures20JavaScript Recursion
Tutorials/JavaScript/JavaScript Function Expressions
🌐JavaScript

JavaScript Function Expressions

Updated 2026-05-12
20 min read

JavaScript Function Expressions

In the previous section, we explored the basics of functions in JavaScript. Now, let's dive deeper into function expressions, including anonymous functions and Immediately Invoked Function Expressions (IIFE). Understanding these concepts is crucial for writing more flexible and efficient code.

Introduction

Function expressions are a way to define functions as values that can be assigned to variables or passed as arguments to other functions. They differ from regular function declarations in how they are hoisted and used. Anonymous functions, which do not have a name, are often used in conjunction with function expressions. Additionally, Immediately Invoked Function Expressions (IIFE) allow you to execute a function immediately after its definition.

Anonymous Functions

An anonymous function is a function without a name. These functions can be assigned to variables or passed as arguments to other functions. They are particularly useful for short-lived operations that don't need to be reused elsewhere in your code.

Example 1: Assigning an Anonymous Function to a Variable

JavaScript
1// script.js
2const greet = function(name) {
3 return 'Hello, ' + name + '!';
4};
5
6console.log(greet('Alice'));
Output

Here, we define a function executeFunction that takes another function as an argument and calls it with the string 'Bob'. We pass an anonymous function to executeFunction, which returns a personalized greeting.

Function Expressions

A function expression is a more flexible way of defining functions compared to declarations. It allows you to assign functions to variables, use them as arguments, or return them from other functions. Function expressions are not hoisted like function declarations, meaning they can only be called after their definition in the code.

Example 3: Basic Function Expression

JavaScript
1// script.js
2const add = function(a, b) {
3 return a + b;
4};
5
6console.log(add(5, 3));
Output

Here, we define a function calculator that takes an operation (another function) and two numbers as arguments. It calls the operation with the two numbers. We pass a function expression that subtracts one number from another to calculator.

Immediately Invoked Function Expressions (IIFE)

An Immediately Invoked Function Expression (IIFE) is a function that runs as soon as it is defined. IIFEs are enclosed in parentheses and followed by an execution operator (). They are often used for creating private scopes, avoiding polluting the global namespace.

Example 5: Basic IIFE

JavaScript
1// script.js
2(function() {
3 console.log('This function runs immediately!');
4})();
Output

Here, we define an IIFE that takes a name parameter and logs a greeting message. We pass the argument 'Charlie' directly to the IIFE.

Example 7: Using IIFE for Private Scope

JavaScript
1// script.js
2(function() {
3 let secret = 'This is a secret!';
4 console.log(secret);
5})();
Output

In this example, we use an IIFE to encapsulate the calculator operations. The IIFE returns an object with methods for addition, subtraction, multiplication, and division. These methods are defined as function expressions within the IIFE.

Summary

  • Anonymous Functions: Functions without a name, often used in assignments or as arguments.
  • Function Expressions: Assigning functions to variables or passing them as arguments.
  • Immediately Invoked Function Expressions (IIFE): Functions that execute immediately after their definition, useful for creating private scopes.

What's Next?

In the next section, we will explore JavaScript Arrow Functions, which provide a more concise syntax for writing functions and have some unique features compared to traditional function expressions. Understanding arrow functions is essential for modern JavaScript development, especially in functional programming paradigms.


PreviousJavaScript FunctionsNext JavaScript Arrow Functions

Recommended Gear

JavaScript FunctionsJavaScript Arrow Functions