Javatpoint Logo
Javatpoint Logo

JavaScript Variable

A JavaScript variable is simply a name of storage location. There are two types of variables in JavaScript : local variable and global variable.

There are some rules while declaring a JavaScript variable (also known as identifiers).

  1. Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
  2. After first letter we can use digits (0 to 9), for example value1.
  3. JavaScript variables are case sensitive, for example x and X are different variables.

Correct JavaScript variables


Incorrect JavaScript variables


Example of JavaScript variable

Let’s see a simple example of JavaScript variable.

Test it Now

Output of the above example


JavaScript local variable

A JavaScript local variable is declared inside block or function. It is accessible within the function or block only. For example:

Or,


JavaScript global variable

A JavaScript global variable is accessible from any function. A variable i.e. declared outside the function or declared with window object is known as global variable. For example:

Test it Now

To know more about global variable (e.g. how global variable is executed by JavaScript execution engine), visit next page.





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA