Variables in JavaScript are made of the operator var (which is a keyword as well) and an identifier.

The variables in JavaScript are loosely typed and as such they can hold any type of data.

The following variables are all valid in JavaScript:

var text;

var text = "Hello";

text = "Hello";

If the variable is not initialized but only declared (i.e. var text;) it will hold a value undefined.

If the operator var is not specified the variable will still be accepted but such approach is not recommended.


The var operator may also be used to define the scope in which the variable may be approached to (local or global).

In this example two variables are defined as a local and a global in scope, respectively:

function myFunction() {

   var text1 = "Hello"; // local variable

   text2 = 10; // global variable;


Such approach is not recommended as the global variable may be accessible outside the function as soon as the "myFunction()" gets executed for the first time. Such a variable is difficult to maintain and work with.

To define more than one variable at the time, the identifiers may be added to the same var operator, and separated by commas; for instance:

var text1 = "Hello", text2 = "World!", zip = 10120;


var text1 = "Hello",

    text2 = World!",

    zip = 10120;


var text1, text2, zip;


Basic JavaScript variables:


›› go to examples ››