Declaring Variables and Arrays in Javascript

A global variable must be declared outside of all functions on the page. Variables declared within a function are local to that function only. Their values may be passed to other functions as parameters in the function call.

var variableA = 10;
var variableB = "Hello";

Arrays

var names = new Array();
names[0] = "Armstrong, Peter" ;
names[1]= "Smith, Alan" ;
names[2]= "Jones, Bill" ;
alert(names.length);
// Will generate an alert box with the number three which is the number of elements in the array

Or

var names =new Array("Armstrong, Peter",Smith, Alan","Jones, Bill");
alert(names.length);
// Will generate an alert box with the number three which is the number of elements in the array

Arrays start numbering at 0.

// Object constructor
//Firstline is an array of property names, Anotherline is an array of values for each property

function recordconstruc(Firstline,Anotherline) {
var fieldnames = new Array();
var eachrecord = new Array();
fieldnames = Firstline.split("~") ;
eachrecord = Anotherline.split("~") ;
var cnt = 0;
for(var i = 0;i < fieldnames.length; i++) {
var fieldvalue = fieldnames[i];
this[fieldvalue] = eachrecord[i] ;
}
} // end of function recordconstruct


Home
Site Index