Using Variables in Javascript

Variables are used to store values (name = "John") or expressions (sum = x + y). Before using a variable, you first need to declare it. You have to use the keyword var to declare a variable like this:
?
1
var name;
You can assign a value to the variable either while declaring the variable or after declaring the variable.
?
1
var name = "John";
OR
?
1
2
3
var name;
name = "John";
Naming Variables
Though you can name the variables as you like, it is a good programming practice to give descriptive and meaningful names to the variables. Moreover, variable names should start with a letter and they are case sensitive. Hence the variables studentname and studentName are different because the letter n in name is different (n and N).
Try this yourself:
This code is editable. Click Run to Execute
1
2
    <html>
3
4
    <head>
5
6
    <title>Variables!!!</title>
7
8
    <script type="text/javascript">
9
10
    var one = 22;
11
12
    var two = 3;
13
14
    var add = one + two;
15
16
    var minus = one - two;
17
18
    var multiply = one * two;
19
20
    var divide = one/two;
21
22
    document.write("First No: = " + one + "<br />Second No: = " + two + " <br />");
23
24
    document.write(one + " + " + two + " = " + add + "<br/>");
25
26
    document.write(one + " - " + two + " = " + minus + "<br/>");
27
28
    document.write(one + " * " + two + " = " + multiply + "<br/>");
29
30
    document.write(one + " / " + two + " = " + divide + "<br/>");
31
32
    </script>
33
34
    </head>
35
36
    <body>
37
38
    </body>
39
40
    </html>
41