Here we are going to learn about the Anonymous functions with their examplaes.
A function is a set of statements that take inputs, do some specific computation, and produce output. Basically, a function is a set of statements that performs some tasks or does some computation and then return the result to the user. The anonymous function works the same as the normal function but they differ in terms of syntax.
An anonymous function is a function that does not have any name associated with it. Normally we use the function keyword before the function name to define a function in JavaScript, however, in anonymous functions in JavaScript, we use only the function keyword without the function name. An anonymous function is not accessible after its initial creation, it can only be accessed by a variable it is stored in as a function as a value. An anonymous function can also have multiple arguments, but only one expression.
Example 1:
// Normal function
function Display() {
return “GeeksforGeeks!”;
}
console.log(Display());
// Anonymous function
let display = function() {
return “GeeksforGeeks!!!”;
}
console.log(display());
Example 2:
let display = function() {
return “GeeksforGeeks…!”;
}
console.log(display());
// Using arrow function
let displayName = () => {
return “GeeksforGeeks….!”;
}
console.log(displayName());