

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a typical use case for JavaScript anonymous functions?
In this article, we are going to explore the Anonymous functions in JavaScript and also learn about their use cases. An anonymous function is a special type of function that does not have any name associated with it.
In JavaScript, we normally use the function <funcName>() keyword before defining any function in JavaScript. However, in anonymous functions in JavaScript, we only use the keyword function for defining the function without any supported name.
An anonymous function cannot be accessed after it is created, it can only be accessed by a variable that is stored in the function as the value. An anonymous function can have multiple arguments but will always be executing a single expression only.
Example 1
In the below example, we have created a Normal function and then a simple Anonymous function. Both the functions are performing the same operation i.e. Printing a message to the console. But the anonymous function does not have a function name and only has a single expression.
#Filename: index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initialscale= 1.0" /> <title>Anonymous Function</title> </head> <body> <h1 style="color: red;"> Welcome To Tutorials Point </h1> <script> // Normal function function Display() { return "Welcome To Tutorials Point!"; } console.log(Display()); // Anonymous function let display = function() { return "SIMPLY EASY LEARNING!!!"; } console.log(display()); </script> </body> </html>
Output
It will produce the following output in the Console.
Welcome To Tutorials Point! SIMPLY EASY LEARNING!!!
Example 2
In the below example, we have created two anonymous functions using arrow and without using arrow. Both the functions are performing sum of two numbers.
#Filename: index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initialscale= 1.0" /> <title>Anonymous Function</title> </head> <body> <h1 style="color: red;"> Welcome To Tutorials Point </h1> <script> // Anonymous Function let add = function (a, b) { return a + b; }; console.log("Sum of 3 & 5 using Anonymous Function is:" + add(3,5)); // Anonymous function using arrow let addV2 = (a, b) => a + b; console.log("Sum of 3 & 5 using Anonymous Function " + "with Arrow is:" + addV2(3,5)); </script> </body> </html>
Output
On successful execution of the above program, it will produce the following output in the Console.
Sum of 3 & 5 using Anonymous Function is:8 Sum of 3 & 5 using Anonymous Function with Arrow is:8
- Related Questions & Answers
- When to use anonymous JavaScript functions?
- JavaScript closures vs. anonymous functions
- JavaScript Encapsulation using Anonymous Functions
- Anonymous Wrapper Functions in JavaScript
- What are Self-Invoking Anonymous Functions in JavaScript?
- What is the difference between anonymous and inline functions in JavaScript?
- Does use of anonymous functions affect performance?
- What is the purpose of wrapping whole JavaScript files in anonymous functions?
- PHP Anonymous functions
- What is an anonymous function in JavaScript?
- What is a typical worst habit of Indians?
- How to pass arguments to anonymous functions in JavaScript?
- What is the use of Higher-order functions in JavaScript?
- What MySQL functions can we use to change the character case of a string?
- What is the use of Math.asinh() and Math.acosh() functions in javascript?