Microsoft® JScript™
Copying, Passing, and Comparing Data
 JScript Tutorial 
 Previous | Next 


In Microsoft JScript, how data is handled depends on its data type.
By Value vs. By Reference
Numbers and Boolean values (true and false) are copied, passed, and compared by value. When you copy or pass by value, you allocate a space in computer memory and put the value of the original into it. If you then change the original, the copy is not affected (and vice versa), because the two are separate entities.

Objects, arrays, and functions are copied, passed, and compared by reference under most circumstances. When you copy or pass by reference, you essentially create a pointer to the original item, and use the pointer as if it were a copy. If you then change the original you change both it and the copy (and vice versa). There is really only one entity; the "copy" is not actually a copy, it's just another reference to the data.


Note  You can change this behavior for objects and arrays by specifying the assign( ) method for them.

Last, strings are copied and passed by reference, but are compared by value.


Tip  Because of the way the ASCII and ANSI character sets are constructed, "Zoo" compares as less than "aardvark." That is, capital letters precede lowercase ones in sequence order.

Passing Parameters to Functions

When you pass a parameter to a function by value, you are making a separate copy of that parameter, a copy that exists only inside the function. If, on the other hand, you pass a parameter by reference, and the function changes the value of that parameter, it is changed everywhere in the script.

Testing Data
When you perform a test by value, you compare two distinct items to see whether they are equal to each other. Usually, this comparison is performed on a byte-by-byte basis. When you test by reference, you are checking to see whether two items are pointers to a single original item. If they are, then they compare as equal; if not, even if they contain the exact same values, byte-for-byte, they compare as unequal.

Copying and passing strings by reference saves memory; but because you cannot change strings once they are created, it becomes possible to compare them by value. This lets you test whether two strings have the same content even if one was generated entirely separately from the other.


© 1997 by Microsoft Corporation. All rights reserved.