|
VariablesA variable is a placeholder for storing and manipulating a value. You'll use variables in many different ways throughout your scripts. If you've taken algebra, you've seen variables. If you haven't taken algebra, don't worry about it. Variables are simply the way JavaScript stores information. For example, if you write
In order to use variable, it is good programming style to declare it. Declaring a variable tells JavaScript that a variable of a given name exists so that the JavaScript interpreter can understand references to that variable name throughout the rest of the script. 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:
Ending Statements with a Semicolon? NOTE: JavaScript doesn't require us to declare variables or end statements with semicolons. However, in the traditional programming languages C, C++, Java, and Perl each code statement must end with a semicolon. By getting into the habit of using semicolons you will have no problems when you get into other languages that require semicolons. The same principle applies to declaring variables. 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:
Here you have declared the variable named example and assigned a string value of "An Example" to it. Because JavaScript allows variables to also be declared on first use, the command example = "An Example" would also achieve the same result. 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 If a JavaScript browser looks at a variable in a script and sees that the data after the = sign is not enclosed in quotation marks, and if it is not the word true or false, it creates a numeric value (also looks for 0 or 0x, otherwise assumes that’s a decimal). 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).
JavaScript is what is called a loosely typed programming language. In loosely typed languages, the type of a literal or variable is not defined when a variable is created and can, at times, change based on the context. By comparison, Java and C are not loosely typed. 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. Fortunately, if you run into a situation where JavaScript is treating numbers like text, there's a Method that can convert text string numbers back into a real numbers. 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.
|