Contents

Comments Objects

Script Arrays

Arrays are used to store lists of variables. You can change an array using it's built in functions.


var Fruit=new Array();
Fruit
[0]="Apple";
Fruit
[1]="Banana";
Fruit
[2]="Orange";
alert("The second fruit is :"+Fruit[1]);

var Veg=new Array("Potato" , "Carrot" , "Peas");

var EmptyArray=new Array(3);

var Colors=new Array();
Colors.push
("Red");
Colors.push
("Orange");
Colors.push
("Yellow");

Script Script

join • Returns a string with all the values in an array joined together.

.join("Separator");

Example:


var Veg=new Array("Potato" , "Carrot" , "Peas");
var Text="Vegetables: "+Veg.join(",");

indexOf • Return the index of an item in the array. If the item is not found, -1 is returned.

.indexOf(Item:Object);

Example:


var Veg=new Array("Potato" , "Carrot" , "Peas");
alert(Veg.indexOf("Carrot"));

insert • Insert new values into an array at an index.

.insert(Index:Number,Item1:Object,Item2:Object,...);

Example:


Veg.insert(2,"Turnip");
Veg.insert
(0,"Pea","Onion");

length • The number of values in an array.

.length;

Example:


alert("There are "+Veg[1].length+" vegatables in the array.");

pop • Remove and return the last value from an array.

.pop();

Example:


var LastValue=Veg.pop();

push • Add new values to the end of an array.

.push(Item1:Object,Item2:Object,...);

Example:


Fruit.push("Grape");
Fruit.push
("Strawberry","Mango");

reverse • Return a new array with the values in reverse.

.reverse();

Example:


var Backwards=Fruit.reverse();

shift • Remove and return the first value from an array.

.shift();

Example:


var FirstValue=Veg.shift();

slice • Return a new array with a slice of the values.

.slice(Start:Number,End:Number);

Example:


var SomeValues=Fruit.slice(2,4);

sort • Sort the array. The compare function is optional.

.sort(Compare:Function);

Example:


Fruit.sort();

var Numbers=new Array(12,2,47,-2,8,0);
function CompareNumbers(NumberA,NumberB){
return NumberA-NumberB;
}
Numbers.sort
(CompareNumbers);

splice • Return a new array, with some values removed and some more values added in place.

.splice(Start:Number,Length:Number,Item1:Object,Item2:Object,...);

Example:


var ChangeFruit=Fruit.splice(1,2,"Date","Pineapple");

unshift • Add new values to the start of an array.

.unshift(Item1:Object,Item2:Object,...);

Example:


Fruit.unshift("Grape");
Fruit.unshift
("Strawberry","Mango");

Comments Objects