Domesticated Brain

JavaScript Variables

 

Variables in JavaScript are also can be described as a container or a storage space that holds information in the memory which can be reused throughout the program

 

Let’s see how we can declare a variable in java script

 

As other programming languages there are some rules to be considered when we declare a variable in JavaScript.

Using of unique names to declare variables and names of the variables can be short names or can be a descriptive name.

Name of the variables can consist of letters, digits, underscores (_) and dollar signs ($) and name should be begun with a (latter, _ , $). Variable 2one is not a valid name because you cannot start a variable name with a number

Names are case sensitive, its mean variable num01 and variable Num01 are different in JavaScript as you will see the following program not running correctly.

 

<html>
<head>
</head>


<body>

  <script type="text/javascript">
          var num01 = 10;
                document.write(Num01);
  </script>

</body>


</html>

 

Reserved words like JavaScript cannot be used as a name of a variable

 

Declaring a Variables

 

To declare a variable in JavaScript we need to use var keyword

Next you will have to write the name of your variable and the assignment operator (To assign a value to the variable we use the equal sign)

Then the value that you want to assign for the variable

 

var num01 = 10;

var num02 = 20;

 

The above example variable num01 stores value 10 and variable num02 stores the value 20

Here is a simple JavaScript program as an example for using variable in JavaScript

 

<html>
<head>
</head>
<body>

   <script type="text/javascript">
      var num01 = 10;
         document.write(num01);
   </script>

</body>
</html>


 

The output of the above JavaScript Program is 10