Skip to content

Instantly share code, notes, and snippets.

@Chalarangelo
Last active September 11, 2017 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chalarangelo/c527a92224c15e46edfb4e0807ef30fc to your computer and use it in GitHub Desktop.
Save Chalarangelo/c527a92224c15e46edfb4e0807ef30fc to your computer and use it in GitHub Desktop.
function someFunction(){
if (true){
// This variable is defined inside these
// curly braces, which means its part of the
// scope of this block and cannot be accessed
// by other parts of someFunction.
let blockVariable = 30;
// However, using the 'var' keyword this variable,
// while defined inside the same block, is part of
// the scope of the someFunction.
var functionVariable = 30;
// Inside the block, both variables are accessible.
console.log("Block scope: ");
console.log(blockVariable);
console.log(functionVariable);
}
// Outside the block, only the variable defined using
// the keyword 'var' is accessible.
console.log("Function scope: ");
try { console.log(blockVariable); }
catch (e) { console.log("The variable 'blockVariable' is not accessible!"); }
try { console.log(functionVariable); }
catch (e) { console.log("The variable 'functionVariable' is not accessible!"); }
}
someFunction();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment