JavaScript - Variables, Naming Rules and Syntax

Variables and Variables Naming Rules, JavaScript - Variables and Variables Naming Rules avaScript variables can be used to hold values [e.g.: (x=10) or expression (a=b+c)]. Variable can have short names (like x and y) or more descriptive names (age, sum total volume) JavaScript variables are declared using the keyword var.


Variables and Naming Rules
  1. JavaScript variables can be used to hold values [e.g.: (x=10) or expression (a=b+c)]. 
  2. Variable can have short names (like x and y) or more descriptive names (age, sum total volume) 
  3. JavaScript variables are declared using the keyword var. 
Rule For Naming Variables
  1. Variable names must begin with a letter. 
  2. Variable names can also begin with $ and _. 
  3. Variable names are case sensitive ( Y and y are different variables). 
  4. Both JavaScript statements and JavaScript variable 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.

JS Code

<script type="text/javascript">
 var case1;
 var case2;
 var case3;
</script>
From the above example we already knew 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>

You May Also Like...