-3

First :

function A() {

        for (var i = 0; i < 10; ++i) {
            //...
        }
        console.log(i); //i is undefined
       // i dont get global i
        for (i = 0; i < 10; ++i) {
            //...
        }

    }

When i execute A() , i donot get global var i . But this:

    function B() {
       // i get global i
        for (i = 0; i < 10; ++i) {
            //...
        }
     }

When i execute B() , I get global i . So why i cannot get the global when i execute A() ?

shaoyi he
  • 107
  • 1
  • 9

1 Answers1

0

i is never globally scoped in your code. It is lexically scoped only within the confines of your A function.

Richard Kho
  • 5,086
  • 4
  • 21
  • 35