JavaScript variables 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 JavaScript Variables
As other programming languages there are some rules to be considered when declaring a variable.
The names of the variables can be unique, short and descriptive
These names 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 is 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 the value 10 and variable num02 stores the value 20
Here is a simple JavaScript program that explains the way of using a 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