JavaScripture
Contribute Feedback

JavaScript Array : Object

Arrays are containers that allow access to its items through numerical indices.

Array() : Array

Creates an empty Array (length = 0). Can also be constructed as [].

Example:

Run

Results:

 

Array(length : Number) : Array

Creates an Array of the specified length where each item starts as undefined.

Example:

Run

Results:

 

Array(item0 : Object, [item1 : Object, [...]]) : Array

Creates an Array with the specified parameters as the 0th, 1st, ... items of the Array. Can also be constructed as [item0, item1, ...].

Example:

Run

Results:

 

Constructors

new Array() : Array

Same as Array().

new Array(length : Number) : Array

Same as Array(length).

new Array(item0 : Object, [item1 : Object, [...]]) : Array

Same as Array(item0, item1, ...).

Instance Indexers

this[index : Number] : Object

Gets and sets the item in this at index. index should be between 0 and this.length - 1. If setting to an index greater than length - 1, length will be increased to index + 1.

Example:

Run

Results:

 

Instance Properties

length : Number

The number of items in this. It is 1 greater than the index of the last item.

Example:

Run

Results:

 

Instance Methods

concat(item0 : Object, [item1 : Object, [...]]) : Array

Returns a new Array composed of the items of this followed by item0, item1, .... If any of the parameters are Arrays themselves, the values of that Array will be concatenated into the new Array.

Example:

Run

Results:

 

every(callback : Function, [thisArg : Object]) : Boolean
callback(item : Object, index : Number, array : Array) : Boolean

Returns true if callback returns true for every item in this. Otherwise returns false. The Array passed to callback is the this of the call to every.

Example:

Run

Results:

 

filter(callback : Function, [thisArg : Object]) : Array
callback(item : Object, index : Number, array : Array) : Boolean

Returns a new Array containing only the items in this that callback returned true for. The Array passed to callback is the this of the call to filter.

Example:

Run

Results:

 

forEach(callback : Function, [thisArg : Object]) : undefined
callback(item : Object, index : Number, array : Array) : undefined

Calls callback for each item in this. The Array passed to callback is the this of the call to forEach.

Example:

Run

Results:

 

indexOf(item : Object, [startingIndex = 0 : Number]) : Number

Returns the first location of item in this starting the search from startingIndex. If startingIndex is negative, this.length is added to it before starting the search. Returns -1 if item is not found.

Example:

Run

Results:

 

join([separator = ',' : String]) : String

Returns a String created by joining the toString() of each item of this separated by separator.

Example:

Run

Results:

 

lastIndexOf(item : Object, [startingIndex : Number]) : Number

Returns the location of item in this starting the search from startingIndex by searching backwards through the array. If startingIndex is not specified, the search starts from the end of the array. If startingIndex is negative, this.length is added to it before starting the search. Returns -1 if item is not found.

Example:

Run

Results:

 

map(callback : Function, [thisArg : Object]) : Array
callback(item : Object, index : Number, array : Array) : Object

Returns a new Array with where each item is the result of calling callback on each item in this. The Array passed to callback is the this of the call to map.

Example:

Run

Results:

 

pop() : Object

Removes the last item from this and returns the removed item. Returns undefined if this is empty. Use with push to treat an Array as a stack.

Example:

Run

Results:

 

push(item0 : Object, [item1 : Object, [...]]) : Number

Appends the specified items to the end of this and returns the new value of this.length. Use with pop to treat an Array as a stack.

Example:

Run

Results:

 

reduce(callback : Function, [initialValue : Object]) : Object
callback(previous : Object, current : Object, index : Number, array : Array) : Object

Calls callback for each item in this and passes the return value of callback as the previous parameter to the next callback invocation. Returns the result from the last call to callback. If initialValue is not specified, callback will first be called on this[1] with previous set to this[0]. The Array passed to callback is the this of the call to reduce.

Example:

Run

Results:

 

reduceRight(callback : Function, [initialValue : Object]) : Object
callback(previous : Object, current : Object, index : Number, array : Array) : Object

Calls callback for each item in this in reverse order and passes the return value of callback as the previous parameter to the next callback invocation. Returns the result from the last call to callback. If initialValue is not specified, callback will first be called on this[this.length - 2] with previous set to this[this.length - 1]. The Array passed to callback is the this of the call to reduceRight.

Example:

Run

Results:

 

reverse() : Array

Reverses the order of the items in this and returns this.

Example:

Run

Results:

 

shift() : Object

Removes the first item of this and shifts the remaining items down by 1. Returns the removed item.

Example:

Run

Results:

 

slice(start : Number, [end : Number]) : Array

Returns a new Array which is composed of the items this[start], this[start + 1], ..., this[end - 1]. Note that item[end] is not included. If start or end is negative, the value is added to this.length before performing the slice. If end is not specified, this.length is used.

Example:

Run

Results:

 

some(callback : Function, [thisArg : Object]) : Boolean
callback(item : Object, index : Number, array : Array) : Boolean

Returns true if callback returns true for at least one item in this. Otherwise returns false. The Array passed to callback is the this of the call to some.

Example:

Run

Results:

 

sort([comparisonFunction : Function]) : Array
comparisonFunction(x : Object, y : Object) : Number

Sort the items of this using comparisonFunction to determine the sort order and returns this. The Number returned by comparisonFunction should be 0 if x and y are equal, negative if x is less than y, or positive if x is greater than y. If comparisonFunction is not specified, the toString() of the items will be sorted in alphanumeric order. Returns this.

Example:

Run

Results:

 

splice(start : Number, count : Number, [item0 : Object, [item1 : Object, [...]]]) : Array

Removes count items from this starting at index start. If the optional items are specified, they are inserted into this at start. Returns a new Array containing the removed items.

Example:

Run

Results:

 

unshift(item0 : Object, [item1 : Object, [...]]) : Number

Inserts the specified items at the start of this. Returns the new value of this.length.

Example:

Run

Results:

 

Array Methods

isArray(value : Object) : Boolean

Returns true if value is an Array.

Example:

Run

Results: