Javascript variables
Variables are a fundamental concept in many computer languages and knowing how to work with them is essential knowledge.
This lesson focuses on:
- What is a variable?
- Declaring variables
- Naming variables
- Printing variables
What is a variable?
A variable is a container which stores information in a computer's memory. The value of a variable can change all throughout a script.
Declaring variables
In Javascript, a variable is declared with the var keyword.
Syntax:
var varName;
Example:
<script type="text/javascript>
var aNumber;
</script>
In the above example, a variable named aNumber is declared. You can assign a value to a variable at the same time that it is declared. This process is known as initialization.
Example of initializing a variable:
<script type="text/javascript>
var aNumber = 5;
</script>
In the above example a variable named aNumber is initialized with the value 5.
Example of declaring a variable and then giving it a value:
<script type="text/javascript>
var aNumber;
aNumber = 5;
</script>
In the above example a variable named aNumber is declared. On the next line the variable aNumber is assigned the value 5.
NOTE: The var keyword should be used only once with each variable - during declaration. After a variable has been declared or initialized, you can refer to it by its name without the var keyword. You can declare variables without the var keyword, as using the var keyword to declare variables is optional, but doing so is good convention.
Naming variables
When naming variables, several rules should be followed:
-
Make sure that the variable name is descriptive.
If you do not give a variable a descriptive name, it will be hard to understand what the variable refers to. For example, if you wanted to create a variable which would hold a value signifying the amount of chairs in a room, which variable name would be more appropriate - numChairs or a? The better choice for the variable name would be numChairs because it is more descriptive.
-
Make sure the variable name is of appropriate length.
Make sure the variable name is long enough to be descriptive, but not too long.
-
Do not use spaces in variable names.
This is a rule that must be followed in Javascript because Javascript does not allow spaces in variable names. Using spaces in variable names will generate an error!
-
Do not use special symbols in variable names such as !@#%^&*
As is the rules with spaces, special symbols are not allowed in variable names. Using special symbols in variable names will generate an error. There is however one special symbol that can be used in variable names, and that symbol is the undersocre ( _ ) symbol. Variable names can only contain letters, numbers and the underscore ( _ ) symbol.
-
Distinguish between uppercase and lowercase letters
Javascript is a case sensitive language! This means that the variables valueOne, ValueOne, and VALUEONE are three separate variables.
-
Be careful about the first character of a variables name
The first character of a variables name must be a letter or an underscore. If the first character of a variable name is not a letter or an underscore, an error will be generated!
Printing variables
Variables are printed by including the variable name in a document.write() command. When printing the value of a variable, the variable name should NOT be included in double quotes.
Example:
<script type="text/javascript">
var numChairs = 5;
document.write(numChairs);
</script>
Output:
5
You can also print variables together with regular text. To do this, use the + symbol to join the text and variable values together.
Example:
<script type="text/javascript">
var teamName = "The Penguins";
var teamState = "Connecticut";
var numWins = 12;
var numLosses = 2;
document.write("The name of the team is " + teamName);
document.write("<br />The team is from " + teamState);
document.write("<br />" + teamName + " have won " + numWins +
" games and have lost " + numLosses + " games");
</script>
Output:
The team is from Connecticut
The Penguins have won 12 games and have lost 2 games




