
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- 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 a Typical Day Like for a Digital Marketer?
- How to pass arguments to anonymous functions in JavaScript?
- What is an anonymous function in JavaScript?
- The Anonymous Functions in Python
- What is the use of Higher-order functions in JavaScript?
- What MySQL functions can we use to change the character case of a string?
