please dont rip this site
Microsoft® JScript™
JScript Variables
 JScript Tutorial 
 Previous | Next 


Variables are used in Microsoft JScript to store values in your scripts. They are a way to retrieve and manipulate values using names. When used effectively then can help in understanding what a script does.

Declaring Variables

Although not required, it is considered good practice to declare variables before using them. You do this 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.

The following code examples are of variable declaration:


var mim = "A man, a plan, a canal, Panama!";  // The value stored in mim is of string type.
// The sentence in quotes, the value of which is assigned to mim, is a string literal.

var ror = 3;        // The value stored in ror has numeric type.
var nen = true;        // The value stored in nen has Boolean type.

var fif = 2.718281828        // The value stored in fif has numeric type.

Naming Variables

JScript is a case-sensitive language, so naming a variable myCounter is different from naming it MYCounter. In addition, variable names, which can be of any length, must follow certain rules:

Some examples of valid variable names:

Some invalid variable names:

In instances in which you want to declare a variable and initialize it, but without giving it any particular value, you may assign it a special value, null.

var zaz = null;
var notalot = 3 * zaz;        // At this point, notalot becomes 0.
If you declare a variable without assigning any value to it, it exists but is undefined.

var godot;
var waitingFor = 1 * godot;  // Places the value NaN in waitingFor as godot is undefined.
You can declare a variable implicitly (without using var) by assigning a value to it. You cannot, however, use a variable that has never been declared at all. To do so generates an error at runtime.

lel = "";  // The variable lel is declared implicitly.

var aMess = vyv + zez;  // Generates an error because vyv and zez don't exist.
Coercion
As JScript is a loosely-typed language, variables in JScript technically have no fixed type. Instead, they have a type equivalent to the type of the value they contain. 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". The following steps are followed to arrive at this result:
  1. Look at the types of 1 and "10". The "10" is a string, the 1 is a number, so the number is coerced into a string.
  2. As the values on either side of the + operator are both strings, do a string concatenation. This results in "110"
  3. Look at the types of the values on either side of the +=. nowWhat contains a number, and "110" is a string, so convert the number to a string.
  4. As there are now strings on either side of the += operator, do a string concatentation. This results in "0110".
  5. Store this result in nowWhat.

var nowThen = 0;
nowThen += 1 + parseInt("10");        // In this case, "+=" performs addition.
After this code is executed, the nowThen variable contains the integer 11.


© 1997 by Microsoft Corporation. All rights reserved.


file: /Techref/inet/iis/jscript/htm/js8.htm, 6KB, , updated: 1997/9/30 03:45, local time: 2024/11/20 01:24,
TOP NEW HELP FIND: 
3.141.201.29: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?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://linistepper.com/techref/inet/iis/jscript/htm/js8.htm"> JScript Variables</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?