Last active
October 3, 2017 23:23
-
-
Save cstroliadavis/2cf93e234a958901a3aacd3125a54f77 to your computer and use it in GitHub Desktop.
JavaScript variable scoping: source https://jsbin.com/kuyidah
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// # Things I wish I knew about JavaScript when I started: | |
// # The basics: | |
// ## variables are traditionally scoped to the function in which they are declared | |
var thisVariableIsGlobal = "Don't do this."; | |
function thisWillHaveLocallyScopedVariables(){ | |
var thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime = Math.random(); | |
console.log(thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime); | |
} | |
// will try to log, but won't be able to. This will error since the variable we are | |
// using has not been declared yet. | |
try { | |
console.log(thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime); | |
} | |
catch(e){ | |
console.log('thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime not only has not been set, it has not even been declared at this scope, so it would error'); | |
} | |
// Now it should log a random number between 0 and 1 | |
thisWillHaveLocallyScopedVariables(); | |
// Now it should be a different random number | |
thisWillHaveLocallyScopedVariables(); | |
// Just to show that it has not been set globally, here you will see it is still not declared or defined in this scope | |
try { | |
console.log(thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime); | |
} | |
catch(e){ | |
console.log('thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime is still not declared. Here is the message:'); | |
console.log(e.message); | |
} | |
// This might actually seem obvious but it IS going somewhere, so bear with me | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// # Things I wish I knew about JavaScript when I started: | |
// # The basics: | |
// ## variables are traditionally scoped to the function in which they are declared | |
var thisVariableIsGlobal = "Don't do this."; | |
function thisWillHaveLocallyScopedVariables(){ | |
var thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime = Math.random(); | |
console.log(thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime); | |
} | |
// will try to log, but won't be able to. This will error since the variable we are | |
// using has not been declared yet. | |
try { | |
console.log(thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime); | |
} | |
catch(e){ | |
console.log('thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime not only has not been set, it has not even been declared at this scope, so it would error'); | |
} | |
// Now it should log a random number between 0 and 1 | |
thisWillHaveLocallyScopedVariables(); | |
// Now it should be a different random number | |
thisWillHaveLocallyScopedVariables(); | |
// Just to show that it has not been set globally, here you will see it is still not declared or defined in this scope | |
try { | |
console.log(thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime); | |
} | |
catch(e){ | |
console.log('thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime is still not declared. Here is the message:'); | |
console.log(e.message); | |
} | |
// This might actually seem obvious but it IS going somewhere, so bear with me</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// # Things I wish I knew about JavaScript when I started: | |
// # The basics: | |
// ## variables are traditionally scoped to the function in which they are declared | |
var thisVariableIsGlobal = "Don't do this."; | |
function thisWillHaveLocallyScopedVariables(){ | |
var thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime = Math.random(); | |
console.log(thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime); | |
} | |
// will try to log, but won't be able to. This will error since the variable we are | |
// using has not been declared yet. | |
try { | |
console.log(thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime); | |
} | |
catch(e){ | |
console.log('thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime not only has not been set, it has not even been declared at this scope, so it would error'); | |
} | |
// Now it should log a random number between 0 and 1 | |
thisWillHaveLocallyScopedVariables(); | |
// Now it should be a different random number | |
thisWillHaveLocallyScopedVariables(); | |
// Just to show that it has not been set globally, here you will see it is still not declared or defined in this scope | |
try { | |
console.log(thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime); | |
} | |
catch(e){ | |
console.log('thisBelongsOnlyToThisFunctionAndWillBeANewInstanceEveryTime is still not declared. Here is the message:'); | |
console.log(e.message); | |
} | |
// This might actually seem obvious but it IS going somewhere, so bear with me |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment