JavaScript Anonymous Functions
Last Updated :
06 Jun, 2025
An anonymous function is simply a function that does not have a name. Unlike named functions, which are declared with a name for easy reference, anonymous functions are usually created for specific tasks and are often assigned to variables or used as arguments for other functions.
In JavaScript, you normally use the function keyword followed by a name to declare a function. However, in an anonymous function, the name is omitted. These functions are often used in situations where you don’t need to reuse the function outside its immediate context.
Note: You'll get a syntax error if you don't use parentheses (). The parentheses are needed to treat the anonymous function as an expression, which returns a function object.
Syntax
The below-enlightened syntax illustrates the declaration of an anonymous function using the normal declaration:
function() {
// Function Body
}
We may also declare an anonymous function using the arrow function technique which is shown below:
( () => {
// Function Body...
} )();
The below examples demonstrate anonymous functions.
For more insights into anonymous functions and functional programming, our JavaScript Course offers in-depth coverage of function expressions and other modern JavaScript features.
Example 1: Defining an anonymous function that prints a message to the console. The function is then stored in the greet variable. We can call the function by invoking greet().
JavaScript
const greet = function () {
console.log("Welcome to GeeksforGeeks!");
};
greet();
Example 2: Passing arguments to the anonymous function.
JavaScript
const greet = function( str ) {
console.log("Welcome to ", str);
};
greet("GeeksforGeeks!");
As JavaScript supports Higher-Order Functions, we can also pass anonymous functions as parameters into another function.
Example 3: Passing an anonymous function as a callback function to the setTimeout()method. This executes this anonymous function 2000ms later.
JavaScript
setTimeout(function () {
console.log("Welcome to GeeksforGeeks!");
}, 2000);
Self-Executing Anonymous Functions
Another common use of anonymous functions is to create self-executing functions (also known as IIFE - Immediately Invoked Function Expressions). These functions run immediately after they are defined.
Example 4: Creating a self-executing function.
JavaScript
(function () {
console.log("Welcome to GeeksforGeeks!");
})();
Arrow functions
ES6 introduced a new and shorter way of declaring an anonymous function, which is known as Arrow Functions.In an Arrow function, everything remains the same, except here we don't need the function keyword also. Here, we define the function by a single parenthesis and then '=>' followed by the function body.
Example 5: This is an example of anonymous function with arrow function.
JavaScript
const greet = () => {
console.log("Welcome to GeeksforGeeks!");
}
greet();
If we have only a single statement in the function body, we can even remove the curly braces.
Example 6:In this example, a simple arrow function with a single expression is used.
JavaScript
const greet = () => console.log("Welcome to GeeksforGeeks!");
greet();
OutputWelcome to GeeksforGeeks!
Example 7: illustrates a self-executing function (IIFE), which immediately invokes itself after being defined:
JavaScript
(() => {
console.log("GeeksforGeeks");
})();
Conclusion
Anonymous functions are a powerful feature in JavaScript that help keep code clean and concise. They are great for one-time tasks like callbacks or event handlers. With the introduction of arrow functions in ES6, writing anonymous functions has become even easier and more readable. While they may not always be reusable, their simplicity and flexibility make them an essential tool in modern JavaScript programming.
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read
Introduction of Firewall in Computer Network A firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa
10 min read