javascript - How does a variable without var keyword in self invoking function works? -


this question has answer here:

var salary = "1000$";  (function () {     console.log("original salary " + salary); //1000$      salary = "5000$";      console.log("my new salary " + salary); //5000$ })();  (function () {     console.log("original salary " + salary); //undefined ??       var salary = "5000$";      console.log("my new salary " + salary); //5000$ })(); 

why , how first , third console logs displaying different outputs ?

this due variable hosting .in second iife function , have declared salary variable , moves top of function , overshadows global salary function

javascript convrts code .see code.

  var salary = "1000$";      (function () {         console.log("original salary " + salary); //1000$          salary = "5000$";          console.log("my new salary " + salary); //5000$     })();      (function () {         var salary ;         console.log("original salary " + salary); //undefined ??           // move opvar salary = "5000$";         salary = "5000$";          console.log("my new salary " + salary); //5000$     })(); 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -