# Changing global variables in JavaScript

Note that the global variable will only change at the time the function is called. If there is no function call, the variable will not change:

`let num = 1; function func() { num = 2; // let was not written - changes external variable console.log(num); } // func(); - function call commented out console.log(num); // shows 1 - variable hasn't changed`

And now let's demonstrate with code that the global variable will change only after the function is called:

`let num = 1; function func() { num = 2; } console.log(num); // shows 1 func(); // change the variable console.log(num); // shows 2`

Determine what will be output to the console without running the code:

`let num = 1; function func() { num = 2; } func(); console.log(num);`

Determine what will be output to the console without running the code:

`let num = 1; function func() { num = 2; } console.log(num);`

Determine what will be output to the console without running the code:

`let num = 1; function func() { let num = 2; } func(); console.log(num);`

Determine what will be output to the console without running the code:

`let num = 1; function func() { let num = 2; } console.log(num); func();`

Determine what will be output to the console without running the code:

`let num = 1; function func() { num = 2; } console.log(num); func();`

Determine what will be output to the console without running the code:

`let num = 1; function func() { num++; } func(); func(); func(); console.log(num);`

Determine what will be output to the console without running the code:

`function func() { num = 2; } let num = 1; console.log(num); func();`

Determine what will be output to the console without running the code:

`function func() { num = 2; } let num = 1; func(); console.log(num);`
←→
↑
en ru