Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript variable scope in if statement

var foo = "test";
if(true) {
    alert(foo); // Interviewer: "What does this alert?" Answer: "test"
    var foo = "bar";
}
alert(foo); // "bar" Interviewer: Why is that? Answer: Because JavaScript does not have block scope
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #variable #scope #statement
ADD COMMENT
Topic
Name
6+7 =