JavaScript – Variables, Naming Rules, and Syntax

Variables, Naming Rules, and Syntax

Variables and Naming Rules

  1. Variables can be used to hold values [e.g.: (x=10) or expression (a=b+c)]. 
  2. Can have short names (like x and y) or more descriptive names (age, sum total volume) 
  3. Declared using the keyword var. 

Rule For Naming Variables

  1. Names must begin with a letter. 
  2. Names can also begin with $ and _. 
  3. Variable names are case sensitive ( Y and y are different variables). 
  4. 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>