var aVar = 1.618 ;
function variableLocation(){
try{
console.log( aVar ); // ???
}catch( e ) {
console.log(e.message ); // ???
}
var aVar = 'Golden Ratio';
}
variableLocation();
The output for line 4 is undefined, neither 1.618 or Golden Ratio. The line 6 is never triggered. So, var aVar = 'Golden Ratio' can be seen as two statements. (1) var aVar; which is moved to the very beginning of the function, and aVar = 'Golden Ration'; stays where it is. "Technology is nothing. What’s important is that you have a faith in people, that they’re basically good and smart, and if you give them tools, they’ll do wonderful things with them.” --- Steve Jobs
JavaScript: When a variable is declared in a function
Labels:
JavaScript