Variable Scope in JavaScript: Explanation & Examples

Instructor: Vignesh Sivabalan
The variables in JavaScript have two kinds of scope: local and global. This lesson features detailed examples and explanations to provide insight into both of these scopes within JavaScript.

Variable Scope Defined

The scope of a variable in JavaScript denotes its accessibility by specifying the location where you can access a variable and determines if you have access rights in that particular context. A JavaScript variable has two types of scopes:

  1. Local scope
  2. Global scope

Unlike other programming languages, JavaScript has a function scope instead of a block-level scope. Each JavaScript function creates its own scope.

JavaScript Local Variables

Local variables are those which are declared inside a JavaScript function. These local variables possess function scope, which makes them only accessible within the function and not visible outside the function.

Local Variable Example

//Lines of code here cannot access the mammalName variable.
function animalName()
{
var mammalName = ''dolphin'';
//Code here can access the mammalName variable.
}

In the example above, the variable mammalName is local to the function animalName(). Only the code inside the function animalName() can access mammalName. The variable loses its scope outside of the function. Thus, the variable has a function or local scope. Local variables are always declared with the keyword ''var''.

A local variable gets created when the JavaScript function begins and is deleted at the end of the function.

JavaScript Global Variables

Variables that are declared outside of a JavaScript function have a global scope and are termed as global variables. All functions and scripts within that application (i.e. a web page) have access to the global variables.

Global Variable Example 1

var mammalName = ''Dolphin'';
//the JavaScript code written here can access the variable mammalName
function animalName()
{
//the JavaScript code here can also access the variable mammalName
}

In the above example, the variable mammalName is declared outside the JavaScript function animalName(). Hence its scope is global.

If a variable is not declared using the keyword ''var'', the value assigned to that variable becomes global, as shown in the following example.

Global Variable Example 2

//JavaScript code here can use mammalName
function animalName()
{
mammalName = ''Dolphin'';
//JavaScript code here can use mammalName
}

In the example above, the variable mammalName is not declared as var mammalName. Thus, the scope of this variable becomes global in this code.

Variable Priority

If a local variable and a global variable are declared using the same name, the highest priority goes to the local variable when accessing the variable within the function (local scope) as shown below.

var name = ''Mary'';
//When the variable name is accessed by code here, the value is ''Mary''
function user()
{
var name = ''Rose'';
//When the variable name is accessed here, it takes the value ''Rose''
}

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support