Using variables in a program

You must declare a variable in a script before you can use it in an expression. If you use an undeclared variable, as shown in the following example, the variable's value will be NaN or undefined, and your script might produce unintended results:

var squared = x*x; 
trace(squared); // NaN
var x = 6;

In the following example, the statement declaring the variable x must come first so that squared can be replaced with a value:

var x = 6;
var squared = x*x; 
trace(squared); // 36

Similar behavior occurs when you pass an undefined variable to a method or function:

getURL(myWebSite); // no action
var myWebSite = "http://www.macromedia.com";

var myWebSite = "http://www.macromedia.com"; 
getURL(myWebSite); // browser displays www.macromedia.com

You can change the value of a variable many times in a script. The type of data that the variable contains affects how and when the variable changes. Primitive data types, such as strings and numbers, are passed by value. This means that the actual content of the variable is passed to the variable.

In the following example, x is set to 15 and that value is copied into y. When x is changed to 30 in line 3, the value of y remains 15 because y doesn't look to x for its value; it contains the value of x that it received in line 2.

var x = 15;
var y = x;
var x = 30;

As another example, the variable inValue contains a primitive value, 3, so the actual value is passed to the sqrt() function and the returned value is 9:

function sqrt(x){
  return x * x;
}

var inValue = 3;
var out = sqrt(inValue);

The value of the variable inValue does not change.

The object data type can contain such a large and complex amount of information that a variable with this type doesn't hold the actual value; it holds a reference to the value. This reference is like an alias that points to the contents of the variable. When the variable needs to know its value, the reference asks for the contents and returns the answer without transferring the value to the variable.

The following is an example of passing by reference:

var myArray = ["tom", "josie"];
var newArray = myArray;
myArray[1] = "jack";
trace(newArray);

The above code creates an Array object called myArray that has two elements. The variable newArray is created and is passed a reference to myArray. When the second element of myArray is changed, it affects every variable with a reference to it. The trace() action sends tom, jack to the Output panel.

In the following example, myArray contains an Array object, so it is passed to function zeroArray() by reference. The zeroArray() function changes the content of the array in myArray.

function zeroArray (theArray){
  var i;
  for (i=0; i < theArray.length; i++) {
    theArray[i] = 0;
  }
} 

var myArray = new Array();
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
zeroArray(myArray);

The function zeroArray() accepts an Array object as a parameter and sets all the elements of that array to 0. It can modify the array because the array is passed by reference.