What is anonymous function?
An anonymous function is a function in JavaScript which don’t have any name. Normally when we define a function in JavaScript we write the function name but for the anonymous function, we don’t have to pass any anonymous function. You can watch video for the same here. If you like then do subscribe for similar content
How to define anonymous function?
To define anonymous function you can simple write function without name as below.
function() {
// ...
}
Above function will show you error. To resolve this error you have to wrap this anonymous function into ()
. This function denotes as function as expression and it return function object. When you run the code you won’t get any output since this anonymous function is not getting called anywhere.
(function() {
console.log("this is anonymous function");
// ...
})
Immediately Invoked function
To call anonymous function you have to again write ()
at the end of anonymous function. What this will do, it will automatically execute whatever logic you have written inside the anonymous function.
(function() {
console.log("this is Immediately invoked function");
// ...
})();
Here we are facing one problem, we can not call this function later on since it executes immediately. So how do we call this function later or how do we reuse anonymous function? I am going to explain you it below
Assigning anonymous function as variable?
Below is the way you can assign anonymous function as variable. You can reuse this function anywhere you want. You can also pass arguments to this anonymous function.
let getName = function () {
console.log("My name is Ajay");
}
getName(); // call getName function
How to pass arguments to immediately invoked function?
// passing string as argument
(function(city) {
console.log("My current city is: " + city);
})('Mumbai');
// passing object as argument
(function() {
console.log("Person name is: " + person.name);
})(person={name: "ajay"});
Arrow function as anonymous function
You can replace normal function definition with the arrow function. If you are not aware of arrow function then you can go through this. This will work same as normal anonymous function.
// arrow function
let getAge = () => {
console.log("current age is 24");
}
getAge(); // call getAge
Passing anonymous function as argument
You can pass anonymous function as argument to another function. This is also called as callback function. This anonymous function executes later on depending upon the use cases. In this scenarios anonymous function will get pushed to callback queue and after one second it will invoke and execute whatever you have written inside it.
setTimeout(function() {
console.log("passing anonymous function as argument.");
}, 1000);
Thanks for reading this blog hope you have liked it. If yes then please clap and follow me for more blogs like this.
Did you enjoy this article? If so, get more similar content by subscribing to Decoded, our YouTube channel!. Thanks for reading. Happy coding.