Array object in JavaScript, reference and interactive test

Before the online test of attributes and methods of the Array object, a description of each.

The Array object was first implemented in JavaScript version 1.1 and has been augmented in version 1.3, that is compatible with the ECMA-262 standard as is the version 1.5 which is described here.

The constructor has two sort of parameters

The constructor supports for argument, either the size of the array, or a list of contained items.

The syntax is:

var x = new Array(number);
var x = new Array(item0, item1, etc...);


var x = new Array("one", "two", 254);

A variable may be an argument as any object:

var y = "demo";
var x = new Array(y);

You can also create a array from a literal:

var a = [ "one", "two", 254 ];

The variable declared in a such way will get the attributes and methods of the Array object.

Items are accessed by an index

Arrays, whether created from a literal or a constructor, are objects. Elements can be accessed by the index as explained in the chapter Arrays and JavaScript.


var x = new Array("one", "two", 254);
document.write(x[2])   // must return 254

Size and regular expression attributes

Array has the following attributes:

int length

int index

For a array that is the result of the test of a regular expression, is the index of the string found.

String input

For a array that is the result of a test of a regular expression is the initial string on which the test is applied.

Assigning an array creates a new reference

If we assign the array b to the variable a, the two variables a and b will point to the same array.

var b = ["one", "two"]
var a = b


To make a copy independent of the original, we use the slice method.

var a = b.slice() 

Trying interactively methods to access items or transform the array

As with any object, you can invoke methods associated with the name of an instance of Array.

var x = new Array();

From a set of strings that you give to create the array, you will test methods of this object.

Enter arguments for the constructor of Array:

The so created object will be used by attributes and methods corresponding to buttons below.


with this array


start end

start remove (Adds items of the array after concat above)

Description of all the methods
void push (x)
Add an element to the end of the array.
var y = x.pop()
Returns the last element in the array, and removes it.
var x =shift()
Returns the first element of the array and removes it.
void unshift(x)
Inserts an element at the beginning of the array by shifting the contents by one position.
Integer i = indexOf (String)
Returns the position of the item given in argument.
If the item is not found in the array, returns -1. Not supported by IE6.
String s = x.join ([separator])
Creates a string of characters from the elements of the array. The array is unchanged.
[separator] is an optional separator in parameter.
Array z = x.concat (y)
Concatenates the contents of array x with array y and returns a new array z.
The original x array is not modified.
void sort(function)
Orders the elements by applying the function given in argument.
Array x = slice(int beginning [, int end])
Returns a subset of the array, which remains unchanged. The second optional value indicates the position of the last element not included. The values ​​(0, x.length) return the entire array, which is equivalent to slice().
The values ​​(0, -1) return the array minus the last element.
Array splice(pos, number [, item0, item1, ...])
Starting from the position pos, deletes the given number of elements in the second argument, and optionallu insert the elements of the list that follows. If 0 have to be removed, at least one element must be added to.
With splice, the content of the array itself is changed. Returns another array of the items removed.
String toString()
Provides a representation in string of characters of the array. Like join() but always with commas like separators.
void reverse()
Reverses the order of elements.
Starting with Internet Explorer 9
var indexOf(value)
Returns the index into the array of the value passed as argument. Returns -1 is the value is not found.
var lastIndexOf(value)
Returns the index from the end of the array.

See also

© 2008-2015