mardi 12 septembre 2017

scope of the variable in JavaScript [duplicate]

This question already has an answer here:

function foo(a) {
      function a(){
        return 8; 
      }
      return a();
      y = 9;          
   } console.log(foo() + " " + y);

This gives an undefined error for variable y.

function foo(a) {
      y =9;
      function a(){
        return 8; 
      }
      return a();
   }console.log(foo()+ " "+ y);

This prints 8 9 in browser's console. if we declare a variable without var keyword than it becomes global variable, why first function does not follow the idea ?




Aucun commentaire:

Enregistrer un commentaire