Array
Overview
Arrays are integral to the JavaScript language.
Arrays are used to store multiple values in a single variable.
Elements within an array can be accessed using the subscript operator:
var x = [1, 2, 3, 4];
var index = 2;
var y = x[index]; // y == 3
Properties
Property | Description |
---|---|
constructor |
Returns the function that created the Array object’s prototype. |
length |
Sets or returns the number of elements in an array. |
prototype |
Allows you to add properties and methods to an object. |
Methods
Method | Description |
---|---|
concat(array1, array2, ...) |
Joins two or more arrays, and returns a copy of the joined arrays |
join(separator = ',') |
Joins all elements of an array into a string |
pop() |
Removes the last element of an array, and returns that element |
push(item1, ...) |
Adds new elements to the end of an array, and returns the new length |
reverse() |
Reverses the order of the elements in an array |
shift() |
Removes the first element of an array, and returns that element |
slice(start, end) |
Selects a part of an array, and returns the new array. ’end’ may be omitted to select all items up to the end of the array. ‘start’ may be negative to select elements from the end of the array. |
sort() |
Sorts the elements of an array. |
splice(index,count,[item1], ...) |
Adds/Removes elements from an array. ‘index’ is the index from which to add/remove, ‘count’ is the number of items to remove, and optionally elements to add at ‘index’ may be supplied. |
toString() |
Converts an array to a string, and returns the result. Same as array.join(’,’). |
unshift(item1, ...) |
Adds new elements to the beginning of an array, and returns the new length |
valueOf() |
Returns the primitive value of an array |