Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Why redeclaring a variable is allowed in an IF statement in JavaScript

$
0
0

I know an if statement doesnt have its own scope like a function, meaning it shares the same scope with the containing context. If so, why am I allowed to redeclare the same variable again?

var foo = 123;if (true) {    console.log(foo) // 123    var foo = 456; // Shouldnt it throw an error if refers to same variable?}console.log(foo) // 456

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>