Array
constructor
JS Home <<
JS Reference <<
Array
In JavaScript you create an array instance using the constructor Array
.
Description
Declares an array that allows us to store collections of information that we may want to manipulate or use later.
An array can be created empty, with a length or with elements.
Syntax
Signature | Description |
---|---|
var aArray = new Array(); | Create an empty array. |
var bArray = new Array(arrayLength); | Create an array with the specified length. |
var cArray = new Array(element0, element1, ..., elementN); | Create an array with specified elements. |
var dArray = [element0, element1, ..., elementN]; | Shorthand array creation with specified elements. |
Parameters
Parameter | Description |
---|---|
arrayLength | A number between 0 and 4,294,967,295 where an array is created with that length:
|
element0, element1, ..., elementN | When two or more arguments are passed an array is created using the arguments passed as the elements of the array. |
Class Properties
Class Property | Description |
---|---|
prototype | Enables property assignment to objects of type Array . |
Instance Properties
Instance Property | Description |
---|---|
constructor | Returns a reference to the Array function that created the prototype. |
length | A positive integer holding the number of elements within an array. |
Class Methods
None.
Instance Methods
By using methods of the Array
object we can also access elements of arrays, manipulate arrays after creation and even create new arrays from existing arrays.
Instance Method | Description |
---|---|
Getter (Accessor) MethodsThese methods DO NOT modify the Array object |
|
concat() | Returns a new array composed of concatenated array(s) and/or value(s). |
join() | Creates a string from the elements within an array. |
slice() | Returns a new array comprising of a section of an existing array. |
toString() | Returns a string composed of concatenated array elements delimited by commas. |
Setter (Mutator) MethodsThese methods DO modify the Array object |
|
pop() | Removes the last element of an array and returns that elements value to the caller. |
push() | Adds element(s) to the end of an array and returns the new length of the array. |
reverse() | Reverses the order of elements within an array. |
shift() | Removes the first element of an array and returns that elements value to the caller. |
sort() | sorts the elements within an array. |
splice() | adds and/or removes elements from within an array. |
unshift() | adds element(s) to the start of an array and returns the new length of the array. |
Accessing Arrays
Arrays can be accessed by naming the array and stating the index for the item in the array you want to retrieve, just be aware that arrays are zero-indexed.
Examples
// Create an array with three elements.
var anArray = new Array('one', 'two', 'three');
alert(anArray + ' - ' + anArray.length);
// Create an array with a length of 10.
var newArrayWithLength = new Array(10);
alert(newArrayWithLength + ' - ' + newArrayWithLength.length);
The array literal syntax can also be used to create arrays by using square brackets [ ]. This method of array creation requires less typing and will be used in these tutorials from now on.
// Create an empty array.
var anEmptyArray = []
// Create an array with elements.
var weekendDays = ['Fri', 'Sat', 'Sun'];
// Create an array with mixed types of number, boolean and string.
var mixedTypeArraysAreFine = [1, true, 'Learn JavaScript'];
alert(anEmptyArray + ' - ' + weekendDays + ' - ' + mixedTypeArraysAreFine)
Related Tutorials
JavaScript Intermediate Tutorials - Lesson 1 - Arrays