Variables

JavaScript variables are loosely typed. The loosely typed means that the same variable can hold any type of data. To define a variable, use the var operator:

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
  <script type="text/javascript">
       var aVariable; 
  </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

This code defines a variable named aVariable. aVariable can hold any value.

The code above defines aVariable without initialization. Without initialization aVariable holds the special value undefined.

In JavaScript it's possible to define the variable and set its value at the same time:

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
  <script type="text/javascript">
       var aVariable = "hi"; 
  </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

To define more than one variable using a single statement:

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
  <script type="text/javascript">
       var aVariable = "hi",found = false, age = 29; 
  </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

The var operator defines a variable local to the scope where it was defined. Variables that are defined in a function are local variables and are available for use only within that function. Variables that are defined directly in the script element are global variables and can be accessed anywhere, including in other scripts.

The following code defines a variable inside function using var. aVariable is destroyed as soon as the function exits:

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
  <script type="text/javascript">
        function test(){ 
            var aVariable = "hi"; //local variable 
            document.writeln(aVariable); 
        }
        test(); 
        document.writeln(aVariable); //error! 
  </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

The following code shows how to share variables between <script> tags.

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
  <script type="text/javascript">
    var myGlobalVar = "A";
    function myFunc(name) {
      var myLocalVar = "B";
      return ("Hello " + name + ". Today is " + myLocalVar + ".");
    };
    document.writeln(myFunc("C"));
  </script>
  <script type="text/javascript">
    document.writeln("I like " + myGlobalVar);
  </script>
</body>
</html>
  
Click to view the demo

We can define a variable globally by omitting the var operator:

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
  <script type="text/javascript">
        function test(){ 
            aVariable = "hi"; 
            document.writeln(aVariable); 
        }
        test(); 
        document.writeln(aVariable);
  </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

By removing the var operator the aVariable becomes global. Strict mode throws a ReferenceError when an undeclared variable is assigned a value.

Home 
  JavaScript Book 
    Language Basics  

Language Basics:
  1. Case-sensitivity
  2. Identifiers
  3. Comments
  4. Strict Mode
  5. Statements
  6. Keywords And Reserved Words
  7. Variables