Lexical Scope In JavaScript
Lexical scope defines the scope of variables. So that is may be referenced from within block. Scope of variable is determined by where the variable is placed, inside block or function or outside the block.
<script>
var x = 10;
function sum(){
var x = 2; // Here x = 2 is in lexical scope of a function.
alert("Variable in side function "+x); // output x = 2
}
sum();
alert("Variable outside the function "+x); // output x = 10
</script>
0 Comments