An array is a container of things usually referenced by number.
Arrays use a lot of literal square brackets []. I am not going to mark them in bold on this page to distinguish them from optional items.
The index of JavaScript arrays are zero-based, i.e. from 0 to length-1. EG: To reference the first (or 0th) element in the myArray object, use myArray[0]; to reference the fifth (or 4th), use myArray[4].
var myObj = new Array() //Default array length is 0
var myObj = new Array(arrayLength) //Max length is (2^32)-1, i.e. 4,294,967,295 var myA = new Array(3); //This array has a length of 3 var x = myA[0]; //x is now null because the 0th element is not initialized myA[5] = 9; //Writing into an out of range index changes the length
Different ways to fill an array:
var myObj = new Array([value0[, value1[, ..., valueN]]])
var myA = new Array(6, new Date(), "hi"); //Arrays may have different data types
myObj[0]=value0; [ ... myObj[N]=valueN; ] myA[0]=6; myA[1]=new Date(); myA="hi";
var myObj = [value0[, value1[, ..., valueN]]] //This syntax is not like c or Java var myA = [6, new Date(), "hi"]; //An array literal
Multi-dimensional arrays are possible. EG: This make a 4x3 array whose elements would be referenced as myA[i][j].
var myA = new Array(4);
for (i=0; i<4; i++) {
a[i] = new Array(3);
}
Since an array is an object, you can actually add properties and methods that are not referenced by number. Arrays with elements referenced by key names (i.e. by property names) are called associative arrays. In which such a case it is actually an object (a container of things referenced by name).
var myObjectVariable = {key0:value0[, key1:value1[, ..., keyN:valueN]]}
var myA = {make:"Honda", model:"Accord", year:1998};
var myA = new Array(); myA['make'] = "Honda"; myA['model'] = "Accord"; myA['year'] = 1998;
.constructor. Specifies the function that creates an object's prototype.
.index. For an array created by a regular expression match, the zero-based index of the match in the string.
.input. For an array created by a regular expression match, reflects the original string against which the regular expression was matched.
.length. Reflects the number of elements in an array. The default array length is 0. One of the more useful applications of the length property is to add to an element to the end of an array. EG:
var myA = new Array(); myA[myA.length] = "0th element"; myA[myA.length] = "1st element";
.prototype. Allows the addition of properties to all objects.
.concat(arrayName2, arrayName3[, ..., arrayNameN]). Joins two or more arrays and returns a new array. EG:[1,2,3].concat([4,5]) //returns [1,2,3,4,5]..join([separator]). Joins all elements of an array into a delimited string. If the separator is omitted, then a comma is assumed.[1,2,3] != [1,2,3];[1,2,3].join() == "1,2,3";[1,21,2004].join("/") == "1/21/2004";var myA = "pet,1,dog".split(",");var myA = "2004-01-21".split("-");var myA = "name@biz.com".split(/[\.\@]/);
//myA is now an array of ["name","biz","com"].pop(). Removes the last element from an array and returns that remaining elements. EG:
[1,2,3].pop() //returns [1,2]..push(element1[, ..., elementN]). Adds one or more elements to the end of an array and returns the new length of the array. EG:[1,2,3].push(4,5) //returns 5..reverse(). Transposes the elements of an array: the first array element becomes the last and the last becomes the first. EG:[1,2,3].reverse() //returns [3,2,1]..shift(). Removes the first element from an array and returns the remaining elements. EG:[1,2,3].shift() //returns [2,3]..slice(beginIndex[,endIndex]). Extracts a section of an array and returns a new array. EG:[1,2,3,4].slice(1,3) //returns [2,3].[1,2,3,4].slice(1) //returns [2,3,4]. No endIndex means slice to the end.[1,2,3,4].slice(1,1) //returns [2,3]. A negative endIndex means slice to an offset from the end.[1,2,3,4].slice(-2) //returns [3,4]. A negative begIndex means slice from the end..splice(index, howManyToRemove, [AddValue][, ..., AddValueN]). Adds and/or removes elements from an array and returns what any elements removed. EG:var myA = [1,3];
var x = myA.splice(1,0,2) //Added: x==undefined, myA==[1,2,3]x= myA.splice(2,1) //Removed: x==3, myA==[1,2]x= myA.splice(0,2,3,2,1) //Removed and Added: x==[1,2], myA==[3,2,1].sort([compareFunction]). Sorts the elements of an array according to a specified function or lexicographically by default. EG:[2,11,3].sort() //returns [11,2,3].toSource(). Returns an array literal representing the specified array; you can use this value to create a new array. Overrides the Object.toSource() method. EGs:
var myA = ["a", "b", "c"]
myA.toSource() //Returns ["a", "b", "c"].
""+myArray..toString(). Returns a comma delimited string representing the array and its elements. Overrides the Object.toString() method. EG: "a,b,c".
.unshift(value1[, ..., valueN]). Adds one or more elements to the front of an array and returns the new array. EG:
[3,4,5].unshift([1,2]) //returns [1,2,3,4,5].
.valueOf(). Returns the primitive value of the array. Overrides the Object.valueOf() method. If the elements are Boolean, then returns Boolean. If the elements are Numbers or Dates, then returns Number. If anything else or mixed, then returns String.
2004-01-29 16:32:08Z