A variable is a placeholder for storing and manipulating a value. You'll use variables in many different ways throughout your scripts.
For example, if you write
Although it is possible to declare variables by simply using them, declaring variables helps to ensure that programs are well organized and helps to keep track of the scope of variables.
You can declare a variable using the var command:
In this line, you have defined a variable named example, which currently has no value. It is also possible to assign value to a variable when you declare it:
Note: the equal sign (=) used in assigning a value to a variable is known as an Assignment Operator. Assignment Operators are discussed later in this session.
var NextWeek; NOTE: case-sensitive
Note: If you try to assign non-numeric data to a variable without putting that text in quotation marks, Navigator wonít like it. Youíll probably get a "xxx is not defined" error message (where xxx is the data you were trying to place into the variable).
Casting - Working with numbers and text combined
You may have noticed that the + sign is used differently depending on the context it's in. If you place it between two numbers, the + sign will add the numbers:
If you put the plus sign between two words, the words will be joined together.
And finally, if you put a word and a number together with the + sign, they will be joined as though they were both words.
Ordinarily, you wouldn't want to do this, so when this happens, it's often the result of a mistake, where a number has previously been converted to a text string. This can happen when a number is retrieved from a cookie or if a number is entered into a form along with text characters.
The parseInt() Method takes as an argument any text string that starts with a numeric character and it returns the number at the beginning as a number. It also removes any text that may have followed the number in the text string. Once the text number is converted back to a number you can safely use the + sign to add it to another number.