Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

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?

1 Answer

0 votes
by (106k points)

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); 

}

  • Local scope

var a = 1;

function two(a) {

alert(a); 

function three() {

var a = 3; 

alert(a);

}

Related questions

0 votes
1 answer
0 votes
1 answer

Browse Categories

...