Introduction
This is the "Advanced JavaScript" article series. In this series, we are learning and explaining various concepts of JavaScript. Here are links to all the articles.
- Advance JavaScript: History and role of JavaScript behind modern web
- Advance JavaScript: play with object in JavaScript
- Advance JavaScript: Function Definition Style in JavaScript
- Advance JavaScript: Understand undefined in JavaScript
- Advance JavaScript: Understand "class"-ical concept of JavaScript
- Advance JavaScript: Implement inheritance in JavaScript
- Advance JavaScript: Callback design pattern and callback function in JavaScript
- Advance JavaScript: Exception handling in JavaScript
- Advance JavaScript: Scope of Variable in JavaScript
- Advance JavaScript: closure in JavaScript
In this article, we will learn about the immediate invocation of a function in JavaScript. In general, functions execute when we call them but sometimes it is necessary to execute some code at the time of page loading. For that purpose, there are a few other solutions like we can write code within document.ready in the jQuery method or we can call a function in the body's onLoad event.
The invocation of a function is another solution. The function will be called when the body is loaded. Let's try to understand it with an example.
Define immediately invoke a function
This is the example of an immediately invoked function. We have defined it within a script tag.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title></title>
- </head>
- <body>
- <script>
- //Immediately invoke function
- (function () {
- console.log("This is Immediately invoked function");
- })();
- </script>
- </body>
- </html>

More than one immediately invoked function
We can define more than one immediately invoked function on the same page and they will all be executed, serially. Have a look at the following code.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title></title>
- </head>
- <body>
- <script>
- //More than one Immediately invoke function
- (function () {
- console.log("This is first function");
- })();
- (function () {
- console.log("This is second function");
- })();
- </script>
- </body>
- </html>





Sreekanth ReddyPosted Dec 30, 2016, 1:54 AM
What is the use of this type of functions ?
Jaganathan BantheswaranPosted Nov 24, 2013, 9:18 AM
Nice. But title can be "Self Executing function in Javascript". This is basic feature of Javascript