JavaScripture
Contribute via GitHub Feedback

JavaScript Array : Object

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

Array() : Array

Same as new Array().

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

Constructors

new Array() : Array

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

Example:

Run

Results:

 

new Array(length : Number) : Array

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

Example:

Run

Results:

 

new 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:

 

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:

 

copyWithin(target : Number, start : Number, [end : Number]) : Array

Copies this[start], this[start + 1], ... this[end - 1] to this[target], this[target + 1], ... this[target + start - end - 1]. If end is not specified, this.length will be used. If end is greater than this.length, it is clamped to this.length. Note, no elements are copied past this.length - 1 (ie, copyWithin() will not increase the length of this). Returns this.

Example:

Run

Results:

 

entries() : Iterator<Array>

Returns an iterator of the index and items in this where the valuess of the iterator are of the form [index : Number, item : Object]. See also values() and keys().

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:

 

fill(value : Object, [start = 0 : Number, [end : Number]]) : Array

Fills this[start], this[start + 1], ... this[end - 1] with value. If end is not specified, this.length is used. Returns this.

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:

 

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

Returns the first item in this where callback returns true. The Array passed to callback is the this of the call to find. See also findIndex().

Example:

Run

Results:

 

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

Returns the index of the first item in this where callback returns true. See also find().

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, [start = 0 : Number]) : Number

Returns the first location of item in this starting the search from start. If start 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:

 

keys() : Iterator<Number>

Returns an iterator of the indicies in this. See also entries() and keys().

Example:

Run

Results:

 

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

Returns the location of item in this starting the search from start by searching backwards through the array. If startingIndex is not specified, the search starts from the end of the array. If start 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:

 

values() : Iterator

Returns an iterator of the items in this. The values function is also returned for this[Symbol.iterator] so you can iterate over this directly to get the values. See also entries() and keys().

Example:

Run

Results:

 

Array Methods

from 2 variants
from(arrayLike : Object, [mapFunction : Function, [thisArg : Object]]) : Boolean
mapFunction(item : Object, index : Number) : Object

Returns a new Array with the elements of arrayLike. from() will return a new Array of length arrayLike.length where the elements are arrayLike[0], arrayLike[1], ..., arrayLike[arrayLike.length - 1].

Example:

Run

Results:

 

from(iterator : Iterator, [mapFunction : Function, [thisArg : Object]]) : Boolean
mapFunction(item : Object, index : Number) : Object

Returns a new Array with the elements of iterator containing the items returned by the iterator.

Example:

Run

Results:

 

isArray(value : Object) : Boolean

Returns true if value is an Array.

Example:

Run

Results:

 

of(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, ...]. Note that of(x) will always create an array of length 1 containing x, while new Array(x) which will create an array of length x if x is an integer.

Example:

Run

Results: