From 7dfe87001b7e6c1ade82f0237c533aabdcbd3506 Mon Sep 17 00:00:00 2001 From: Mark Crowther Date: Mon, 13 Jul 2020 15:19:57 +0100 Subject: [PATCH] Adde variable scope examples --- README.md | 2 +- variable-scope-by-example/let-scope-by-example.js | 11 +++++++++++ .../variable-scope-by-example.js | 13 +++++++------ 3 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 variable-scope-by-example/let-scope-by-example.js diff --git a/README.md b/README.md index ce8d73b..c0b0e70 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ If there's anything that you think could be improved due to misunderstanding, th # The 1000 Things Structure of the repo Things are as *folder > main js file*, along with the supporting HTML and CSS files. -(9) Scope example > variable-scope-by-example.js
+(9) Variable and let scope example > variable-scope-by-example.js / let-scope...
(8) Maths Hero > updated-maths-engine.js
(7) Game Sounds > soundPlayer.js
(6) Name Changer > namechange.js
diff --git a/variable-scope-by-example/let-scope-by-example.js b/variable-scope-by-example/let-scope-by-example.js new file mode 100644 index 0000000..712a268 --- /dev/null +++ b/variable-scope-by-example/let-scope-by-example.js @@ -0,0 +1,11 @@ +let aVariableOutsideACodeBlock = 10; + + if (aVariableOutsideACodeBlock == 10) { + let aVariableINSIDE = 20; + console.log("aVariableOutsideACodeBlock called from INSIDE the code block is : " + aVariableOutsideACodeBlock); + console.log("aVariableINSIDE called inside and set inside is: " + aVariableINSIDE); + } +// The below works if we use var, change the var keywords to let and it will fail +// This demonstrates the better scope control of let +console.log("## We now call a let set inside a block, but from the outside, so should see an error"); +console.log("aVariableINSIDE called from OUTSIDE a code block, but set INSIDE that code block is: " + aVariableINSIDE); \ No newline at end of file diff --git a/variable-scope-by-example/variable-scope-by-example.js b/variable-scope-by-example/variable-scope-by-example.js index ce79eab..b5b87db 100644 --- a/variable-scope-by-example/variable-scope-by-example.js +++ b/variable-scope-by-example/variable-scope-by-example.js @@ -1,10 +1,11 @@ var aVariableOutsideACodeBlock = 10; -if (aVariableOutsideACodeBlock == 10) { - var aVariableINSIDE = 20; - console.log("(aVariableOutsideACodeBlock) equals " + aVariableOutsideACodeBlock); - console.log("(aVariableINSIDE) equals " + aVariableINSIDE); -} + if (aVariableOutsideACodeBlock == 10) { + //var aVariableOutsideACodeBlock = 20; + var aVariableINSIDE = 30; + console.log("aVariableOutsideACodeBlock called from INSIDE the code block is : " + aVariableOutsideACodeBlock); + console.log("aVariableINSIDE called inside and set inside is: " + aVariableINSIDE); + } // The below works if we use var, change the var keywords to let and it will fail // This demonstrates the better scope control of let -console.log("(aVariableINSIDE) called from outside equals " + aVariableINSIDE); \ No newline at end of file +console.log("aVariableINSIDE called from OUTSIDE a code block, but set INSIDE that code block is: " + aVariableINSIDE); \ No newline at end of file