My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Immediately invoked functions in javascript.

anil kumar chaudhary's photo
anil kumar chaudhary
·May 4, 2016

I have this example code:

 var salary = "1000$";

 (function () {
     console.log("Original salary was " + salary);

     var salary = "5000$";

     console.log("My New Salary " + salary);
 })();

I found out that the first console.log will print undefined as the salary value. and the second console.log will print 5000$ as the salary.

So, by executing this code, I understand that immediately invoked function get invoked in the first stage of javascript compilation when function and variables are put in variable object inside execution context. Am I right?