Add proper indentation to example code (#23794)

This commit is contained in:
Eric Jae-Min Joo
2018-11-30 07:21:49 -05:00
committed by Manish Giri
parent 521138726b
commit 9621e372d0

View File

@ -5,23 +5,22 @@ title: Global Variables
Global variables are declared outside of a function for accessibility throughout the program, while local variables are stored within a function using `var` for use only within that function's [scope](https://developer.mozilla.org/en-US/docs/Glossary/Scope). If you declare a variable without using `var`, even if it's inside a function, it will still be seen as global:
```javascript
var x = 5; //global
var x = 5; //global variable
function someThing(y) {
var z = x + y;
console.log(z);
var z = x + y;
console.log(z);
}
function someThing(y) {
x = 5; //still global!
var z = x + y;
console.log(z);
x = 5; //still a global variable!
var z = x + y;
console.log(z);
}
function someThing(y) {
var x = 5; //local
var z = x + y;
console.log(z);
var x = 5; //local variable
var z = x + y;
console.log(z);
}
```
A global variable is also an object of the current scope, such as the browser window: