scope

scope

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

var x = 10;

var y = 20;

 

function foo() {

  console.log(x, y);

}

 

foo(); // 10, 20

 

function bar() {

  var y = 30;

  console.log(x, y); // 10, 30

  foo(); // 10, 20

}

 

bar();

In the example, variable x lexically defined in the global scope — that means at runtime it is resolved also in the global scope, i.e. to value 10.



Report Page