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
TypeScript Anonymous Functions Type In TypeScript, an Anonymous Function Type defines a function without a specific name, specifying parameters and return types. This allows for flexible and reusable function definitions, enabling the assignment of functions to variables and the use of type annotations for parameters and return values
3 min read
JavaScript Function Examples A function in JavaScript is a set of statements that perform a specific task. It takes inputs, and performs computation, and produces output. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different
3 min read
JavaScript Function Definitions JavaScript functions are declared using the function keyword, either as a declaration or expression. Declarations define named functions, while expressions assign functions to variables. Both enable code reuse and modularity.SyntaxFunction Declarationsfunction functionName( parameters ) { // Stateme
2 min read
JavaScript Function() Constructor The JavaScript Function() constructor is used to create new function objects dynamically. By using the Function() constructor with the new operator, developers can define functions on the fly, passing the function body as a string. This allows for greater flexibility in situations where functions ne
2 min read
Functions in JavaScript Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.JavaScriptfunction sum(x, y) { return x + y; } console.log(sum(6, 9));Output15 Function Syntax and W
5 min read
JavaScript Function Expression 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.Function expressions can be named or anonymous.They are not hoisted, meaning they are accessible only after their definition.Freq
3 min read
JavaScript Function Invocation In JavaScript, function invocation refers to executing the code defined in a function. Function invocation occurs when a function is called or executed in JavaScript. When invoked, the code inside the function block is run, and any return value is computed and passed back to the caller.JavaScriptfun
3 min read
Interesting Facts about JavaScript Functions Let us talk about some interesting facts about JavaScript Functions that can make you an efficient programmer.Functions Are First-Class CitizensJavaScript treats functions as first-class citizens, meaning they can be:Stored in variablesPassed as arguments to other functionsReturned from other functi
3 min read
JavaScript Course Functions in JavaScript Javascript functions are code blocks that are mainly used to perform a particular function. We can execute a function as many times as we want by calling it(invoking it). Function Structure: To create a function, we use function() declaration. // Anonymous function function(){ // function...body } /
4 min read
Difference between Anonymous and Named functions in JavaScript In JavaScript or in any programming language per say, functions, loops, mathematical operators and variables are the most widely used tools. This article will tell you about the difference between anonymous functions and named functions. We will discuss all the required concepts in this article to k
4 min read