Variables and Naming Rules
- Variables can be used to hold values [e.g.: (x=10) or expression (a=b+c)].
- Can have short names (like x and y) or more descriptive names (age, sum total volume)
- Declared using the keyword var.
Rule For Naming Variables
- Names must begin with a letter.
- Names can also begin with $ and _.
- Variable names are case sensitive ( Y and y are different variables).
- Both JavaScript statements and JavaScript variables are case-sensitive.
You declare JavaScript variables with the var keyword. When you assign a numeric value to a variable, do not put quotes around the value. When you assign a text value to a variable, put double or single quotes around the value.
Javascript Code
<script type="text/javascript">
var case1;
var case2;
var case3;
</script>
From the above example, we already know that variables in JavaScript are generally declared inside the <script> tags.
Multiple Variable Declaration
JS Code
<script type="text/javascript">
var case1,case2,case3;
</script>
Variable initialization can be done in two ways:
- At the time of variable declaration
- At the later point when the variable is required
JS Code
<script type="text/javascript">
var case1="harish";
var case2="satya";
var case3;
case3=2000;
</script>