From d86dd160f29e14ec50c69a4c705c73e59a6486f2 Mon Sep 17 00:00:00 2001 From: Derrek Gass Date: Mon, 12 Oct 2020 13:48:38 -0700 Subject: [PATCH] global scope --- solutions/day-01/scope.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/solutions/day-01/scope.js b/solutions/day-01/scope.js index a8da3ac..15be456 100644 --- a/solutions/day-01/scope.js +++ b/solutions/day-01/scope.js @@ -1,4 +1,5 @@ //scope.js +/* a = 'JavaScript' // is a window scope this found anywhere b = 10 // this is a window scope variable function letsLearnScope() { @@ -6,4 +7,39 @@ function letsLearnScope() { if (true) { console.log(a, b) } -} \ No newline at end of file +} +*/ +/* +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