Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<br>
(9) Variable and let scope example > variable-scope-by-example.js / let-scope...<br>
(8) Maths Hero > updated-maths-engine.js<br>
(7) Game Sounds > soundPlayer.js<br>
(6) Name Changer > namechange.js<br>
Expand Down
11 changes: 11 additions & 0 deletions variable-scope-by-example/let-scope-by-example.js
Original file line number Diff line number Diff line change
@@ -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);
13 changes: 7 additions & 6 deletions variable-scope-by-example/variable-scope-by-example.js
Original file line number Diff line number Diff line change
@@ -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);
console.log("aVariableINSIDE called from OUTSIDE a code block, but set INSIDE that code block is: " + aVariableINSIDE);