How to execute function immediately in Javascript

In this article, we will learn how to execute function immediately in Javascript. To run function directly in JavaScript after defining it, you just have to wrap the function in parenthesis () and invoke it.

Example: Execute function immediately

(function demoFunction() {
 console.log("Immediately Executing Function"); }
)();

The only way to create a new variable scope in JavaScript is by using a function.

EXAMPLE: HOW TO CREATE A NEW VARIABLE SCOPE USING A FUNCTION.

var demo=100;
if(true){
(function() {
var demo=200;
})();
}
console.log(demo);

Leave a Reply

Your email address will not be published. Required fields are marked *

Shopping cart