Back
What is the scope of variables in javascript? Do they have the same scope inside as opposed to outside a function? Or does it even matter? Also, where are the variables stored if they are defined globally?
The scope in Javascript is used to establish the scope for a given function. Typically there is one global scope, and every defined function has its own nested scope.
See the example below:-
A globally-scoped variable
var a = 1;function one() { alert(a); }
var a = 1;
function one() {
alert(a);
}
Local scope
var a = 1;function two(a) {alert(a); } function three() {var a = 3; alert(a);}
function two(a) {
function three() {
var a = 3;
31k questions
32.8k answers
501 comments
693 users