Skip to content

Instantly share code, notes, and snippets.

@Bambina-zz
Created July 19, 2015 11:53
Show Gist options
  • Save Bambina-zz/ffd0e33a253522c0cdc4 to your computer and use it in GitHub Desktop.
Save Bambina-zz/ffd0e33a253522c0cdc4 to your computer and use it in GitHub Desktop.
js block scope (before ES6)
(function x(){
var local = "local";
if(true){
console.log(local); //=>local
var block_scope = "block_scope";
};
console.log(block_scope); //=>block_scope が出力される。上のifブロックで定義した変数block_scopeの中身が出力されている。
})();
console.log(block_scope); //=>ReferenceError: block_scope is not defined 当たり前ですが、上の関数内で定義されたローカル変数は関数内でしか参照できません。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment