DEV Community

Felix Twoli
Felix Twoli

Posted on

Scopes of variables JavaScript

Scope reffers to extent at which Variables/variable is accesible within a program
There are mainly two types of variables scopes:
1.local variables
2.Global varibles
Local Variable:
When you use JavaScript, local variables are variables that are defined within functions. They have local scope, which means that they can only be used within the functions that define them.

In contrast, global variables are variables that are defined outside of functions. These variables have global scope, so they can be used by any function without passing them to the function as parameters.
Global Variable: Since global variables are defined outside there function so variables with the same name can not be used in different functions.
All the scripts and functions on a web page can access it.

Top comments (2)

Collapse
 
nirbhayparmar profile image
Nirbhay Parmar

nice and quick summary of scope of variables in javascript.

Collapse
 
ftwoli profile image
Felix Twoli

Cheers bro though I'm just newbie here.