Microsoft® JScript JScript Variables |
| Tutorial |
|
Variables are used in JScript to store values in your scripts. They allow a way to store, retrieve, and manipulate values using names that may help in understanding what a script does.
Although not required, it is considered good practice to declare variables before using them. This is done using the var statement. The only time you must use the var statement is when declaring variables that are local to a function. At all other times, using the var statement to declare variables before their use is a recommended practice.
Some examples of variable declaration:
var mim = "A man, a plan, a canal, Panama!"; // The variable mim is cast to string type. // The sentence in quotes, the value of which is assigned to mim, is a string literal. var ror = 3; // The variable ror is cast to numeric type. var nen = true; // The variable nen is cast to Boolean type. var fif = 2.718281828 // The variable fif is cast to numeric type.
Variable names must be of a certain format:
- The first character must be a letter (either upper- or lower-case) or an underscore ("_").
- Subsequent characters can be letters, numbers, or underscores.
- The variable name can't be a reserved word.
JScript is case-sensitive, so the variable myCounter is different from MYCounter. Some examples of legal variable names:
- _pagecount
- Part9
- Number_Items
Some illegal variable names:
A special case is provided for instances in which you want to declare a variable and, in some sense, initialize it, but not give it any particular value; this special case is the null value.
- 99Balloons // starts with a number
- Smith&Wesson // '&' is not a legal character for variable names
var zaz = null; var notalot = 3 * zaz; // At this point, notalot becomes 0.If you declare a variable without assigning any value whatever to it, it exists but is undefined.
var godot; var waitingFor = 1 * godot; // Generates an error because godot is undefined.You can declare a variable implicitly by assigning a value to it. You cannot, however, use a variable that has never been declared at all.
lel = ""; // The variable lel is declared implicitly here. var aMess = vyv + zez; // The variables vyv and zez have never been declared, and do not exist. // Their use generates an error at run-time.As JScript is a loosely-typed language, variables in JScript technically have no type. They can be thought of has having a type equivalent to the data they hold. This is important for when variables holding differing types of data are used in the same expression. This is called coercion.
It is possible, under some circumstances, to force the automatic conversion (or coercion) of a variable or a piece of data into a different type. Numbers can easily be included in strings, but strings cannot be included directly in numbers, so explicit conversion functions, parseInt() and parseFloat(), are provided.
var theFrom = 1; var theTo = 10; var doWhat = "Count from "; doWhat += theFrom + " to " + theTo + ".";After this code is executed, the doWhat variable contains "Count from 1 to 10." The number data have been coerced into string form.
var nowWhat = 0; nowWhat += 1 + "10"; // In this case, because "10" is a string // the "+=" operator concatenates.After this code is executed, the nowWhat variable contains "0110" because string data cannot be coerced into numeric form.
var nowThen = 0; nowThen += 1 + parseInt("10"); // In this case, "+=" adds.After this code is executed, the nowThen variable contains the integer 11.
© 1996 by Microsoft Corporation.
file: /Techref/language/asp/js/306.htm, 5KB, , updated: 1996/11/22 10:12, local time: 2024/12/18 22:43,
3.145.40.139:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.org/techref/language/asp/js/306.htm"> Microsoft® JScript Language Tutorial </A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to massmind.org! |
.