Why does this work in javascript?
问题 Just now,I saw some code like this: if(condition){ var xx='sss'; } //do something if(condition){ console.info(xx); } Now, I just wonder why the second if statement work? How can it access the xx variable since it is a local variable defined in another if statement? 回答1: var in JavaScript is scoped to the containing execution context (e.g., the whole function's scope, or the whole global scope if the var is at global scope), not the block. JavaScript doesn't (yet) have block scope (ECMAScript6