Reputation: 41
How do i get the value of the global variable inside a scope . If i have the same name variable present in same scope .
<script>
var number =2;
var fun= function(numbs){
console.log(number);
//here it displays 2
var number =numbs;
console.log(number);
//here it displays 3
console.log(number);
//how do i get value of global variable here
}
fun(3);
</script>
Upvotes: 1
Views: 98
Reputation: 382167
Simply :
console.log(window.number);
From the MDN :
Global variables
Global variables are in fact properties of the global object. In web pages the global object is window, so you can set and access global variables using the window.variable syntax.
But you shouldn't have to do this. If you need to go around shadowing, you have a design problem and you should probably fix it.
Upvotes: 1
Reputation: 9224
you should be able to call
window.number
A global variable is really just a property of the window object.
Upvotes: 6