local variables - Does javascript implement lexical scoping? -


why return 2 instead of 1? seems second "var" silently ignored.

function foo() {   var local = 1;   {     var local = 2;   }   return local; } foo() /* 2 */ 

in javascript there function level scope , global scope. cannot create block scope , adds no special meaning , not create scope.

and how code ends up

function foo() {   var local = 1;   local = 2;   return local; } foo(); 

in es6 can create block level scopes of let. es6 not supported yet. more on here


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -