global scope

pull/73/head
Derrek Gass 5 years ago
parent a0c3f6a932
commit d86dd160f2

@ -1,4 +1,5 @@
//scope.js //scope.js
/*
a = 'JavaScript' // is a window scope this found anywhere a = 'JavaScript' // is a window scope this found anywhere
b = 10 // this is a window scope variable b = 10 // this is a window scope variable
function letsLearnScope() { function letsLearnScope() {
@ -6,4 +7,39 @@ function letsLearnScope() {
if (true) { if (true) {
console.log(a, b) console.log(a, b)
} }
} }
*/
/*
let a = 'JavaScript' // is a global scope it will be found anywhere in this file
let b = 10 // is a global scope it will be found anywhere in this file
function letsLearnScope() {
console.log(a, b) // JavaScript 10, accessible
if (true) {
let a = 'Python'
let b = 100
console.log(a, b) // Python 100
}
console.log(a, b)
}
letsLearnScope()
console.log(a, b) // JavaScript 10, accessible
*/
let a = 'JavaScript' // is a global scope it will be found anywhere in this file
let b = 10 // is a global scope it will be found anywhere in this file
function letsLearnScope() {
console.log(a, b) // JavaScript 10, accessible
let c = 30
if (true) {
// we can access from the function and outside the function but
// variables declared inside the if will not be accessed outside the if block
let a = 'Python'
let b = 20
let d = 40
console.log(a, b, c) // Python 20 30
}
// we can not access c because c's scope is only the if block
console.log(a, b) // JavaScript 10
}
letsLearnScope()
console.log(a, b) // JavaScript 10, accessible

Loading…
Cancel
Save