- Variables must start with a letter*
- Variables can only contain letters, numbers or an underscore (a few other characters are permitted in special circumstances.)
- No spaces
*there are some rare exceptions not addressed here.
Variable names can be a single letter, like i, j or k. But it is often better to use more descriptive names like total, sum or totalscore.
The VAR keyword
var i var total var numberofphotos
Only use the var keyword the first time you mention a variable in any scope (space).
var x = 7
means that we are storing the value 7 inside the variable x. It does not establish equivalency, instead the equals sign is performing a task of storing the value on the right inside the variable on the left.
It is also possible to replace the value of variable with something else. Thus the expression:
x = 7 x = 8 x = 9
means that x will be 9, the final value. Each time we assign a new value, it replaces the old value.
When a variable is assigned to another variable, it receives its value, as in this example:
x = 8 y = x
The value of y is 8. Because in the first line, we assigned 8 to the x variable. Then in the second line, we assigned the value of x, to y.
x = 5 + 7
The value of x will be 12. Likewise, you can use other variables for a part of these math operations.
x = 5 y = 2 j = x + y
In this case the value of j will be 7. You can also perform multiplcation and division, and even use parethesis much the same way you do with algebraic expressions. Multiplication is done with an asterisk (*), and division with a forward slash (/).
x = 9 y = (x / 4) * (x + 3)
The value of y is 27, or 9 divided by 4, multiplied by the sum of 9 and 3.
var x = 7; var i = 10; var total = 22;
String Data Type
Variables can store more than numbers. They can also store words, phrases and several other types of abstract data. If you want to store a word or a phrase, you need to surround it by quotes. (both single or double quotes work.)
var myname = "Jeremy"; var website = "Knight Digital Media Center";
If you don't use quotes, the browser will think you are trying assign values to another variable.
firstname = "Jeremy"; lastname = "Rue"; fullname = firstname + lastname;
sentence = "This tutorial is written by"; firstname = "Jeremy"; lastname = "Rue"; fullsentence = sentence + " " + firstname + " " + lastname;
In this example, the fullsentence variable will contain the string This tutorial is writtten by Jeremy Rue, with spaces between all words.
Concatenating strings with numbers
It is also possible to concatenate strings and number. The result will always be a string.
firstnumber = "4"; secondnumber = 5; fullnumber = firstnumber + secondnumber;
In this example, the value of fullnumber would be the string "45." It's important to understand when variables are numbers, and when they are strings of text that simply contain numbers. Taking the example above, if we didn't have quotes around the 4, it would not concatenate the two values, but rather combine them.
firstnumber = 4; secondnumber = 5; fullnumber = firstnumber + secondnumber;
In this example, the value of fullnumber is 9. Because the type of data in both of these variables are numbers, they are not concatenated, but rather combined as numbers.
Boolean Data Type
var showtooltip = false;
Assigning a Variable to Itself
Sometimes you want to change the value of a variable, based on its current value. This often done by assigning a variable to itself, and applying the changes.
var x = 27; x = x + 3;
In the above example, the value of x is now 30. When you assign a variable to itself, the value doesn't change until the operation is complete. It is also possible to do this with strings, which concatenates the variable:
var fullname = "Jeremy"; fullname = fullname + " " + "Rue";
In the above example, the value of full name is Jeremy Rue. We first assign it the value Jeremy, then we assign the original value with the last name concatenated.