Block scope and Function scope


Javascript ES6,ES7,ES8

Var -> Function scope
Let, Const -> Block scope

1.Var has Function scope and console.log can access hello.

if(true){
  var hello = 'hi';
}
console.log(hello);
// result : hi

2.let has Block scope and console.log cannot access hello.
because let hello is nested by {} (Block).

if(true){
  let hello = 'hi';
}
console.log(hello);
// result : error

3.Var has Function scope and console.log cannot access hello.
because Var hello is nested by function.

function(){
  var hello = 'hi';
}
console.log(hello);
// result : error





© 2017. by isme2n

Powered by aiden