All TalkersCode Topics

Follow TalkersCode On Social Media

Anonymous Function In JavaScript

Last Updated : Jan 1, 2023

Anonymous Function In JavaScript

In this tutorial we will show you the solution of anonymous function in JavaScript, 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. Here we defined three types of anonymous function using normal techniques with example.

Step By Step Guide On Anonymous Function In JavaScript :-

Here we defined three types of anonymous. In first example we used arrow function technique, second example we passed argument to the anonymous function call and in third example we pass an anonymous function as a callback function to the setTimeout() method.

Within each function printed on console using ‘console.log()’ method.

The console.log() method prints any message that need to be displayed to the user.

<!DOCTYPE html>
                console.log("WITHOUT NAME ITSELF");
                console.log("SET TIME OUT FUNCTION");
            var msg=function(txt){
                console.log("PASSED ARGUMENT TO ",txt);
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Within <script> tag we defined three types of anonymous, first type function had no name defined with arrow function technique with message ‘WITHOUT NAME ITSELF’.
  7. Second type anonymous function defined with setTimeout() method for 2000ms later and within that we printed message ‘SET TIME OUT FUNCTION’.
  8. In third type we passed arguments to anonymous function with message ‘PASSED ARGUMENT TO ",txt’.
  9. All messages printed using ‘console.log()’ method. Console.log() is a function in javascript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Its output display on the javascript console.
  10. In browser for open javascript console use the shortcut ‘ctrl+shift+j’ then console panel will open at right hand side there we can see our result.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion we are able to know about types of anonymous functions in javascript.

When we execute our program on browser we can’t see the result so we have use that shortcut ‘ctrl+shift+j’ then result will see three messages printed on console by console.log().

The second method output message will printed in lastly because it will loads after 2000ms so it will printed at last.

The console.log() method only will print any message or value on the console, for general printing, value or string on webpage in javascript we had many inbuilt methods.

We will see about them in future. I hope this tutorial on anonymous function in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials