Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Anonymous Functions
Next article icon

JavaScript Anonymous Functions

Last Updated : 06 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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(); 

Output
Welcome 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.


Next Article
JavaScript Anonymous Functions

A

argha_c14
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-functions

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences