Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Array Object

JavaScript Array Object


The purpose of an array object is to store a series of values under a single variable name.


Online example

Create an array and assign values to it:

Example

var mycars = new Array();

mycars[0] = "Saab";

mycars[1] = "Volvo";

mycars[2] = "BMW";

Try it »

You can find more examples at the bottom of the page.


What is an array?

An array object stores a series of values under a single variable name.

If you have a set of data (for example, car names) stored in separate variables, as shown below:

var car1="Saab";

var car2="Volvo";

var car3="BMW";

However, if you want to find a particular car among them, not 3 cars but 300, that would not be an easy task!

The best approach is to use an array.

An array can store all values under one variable name, and any value can be accessed through that variable name.

Each element in an array has its own ID so that it can be accessed easily.


Create an array

There are three ways to create an array.

The following code defines an array object named myCars:

1: Regular way:

var myCars=new Array();

myCars[0]="Saab";      

myCars[1]="Volvo";

myCars[2]="BMW";

2: Concise way:

var myCars=new Array("Saab","Volvo","BMW");

3: Literal:

var myCars=["Saab","Volvo","BMW"];


Accessing Arrays

By specifying the array name and an index number, you can access a particular element.

The following example accesses the first value of the myCars array:

var name=myCars[0];

The following example modifies the first element of the myCars array:

myCars[0]="Opel";

lamp [0] is the first element of the array. [1] is the second element.

You can have different types of objects in one array

All JavaScript variables are objects. Array elements are objects. Functions are objects.

Therefore, you can have different variable types in an array.

You can include object elements, functions, and arrays in an array:

myArray[0]=Date.now;

myArray[1]=myFunction;

myArray[2]=myCars;


Array methods and properties

Use the predefined properties and methods of the array object:

var x=myCars.length // number of elements in myCars

var y=myCars.indexOf("Volvo") // index of the "Volvo" value


Complete array object reference

You can refer to this site's complete reference for all properties and methods of arrays.

The reference contains descriptions of all properties and methods (and more examples).

Complete array object reference


Creating new methods

The prototype is a global JavaScript constructor. It can construct properties and methods for new JavaScript objects.

Example: create a new method.

Array.prototype.myUcase=function(){

    for (i=0;i

        this[i]=this[i].toUpperCase();

    }

}

Try it »

The example above creates a new array method that converts lowercase characters in an array to uppercase.


More examples

Combine two arrays - concat()

Combine three arrays - concat()

Make a string from the elements of an array - join()

Remove the last element of an array - pop()

Add a new element to the end of an array - push()

Reverse the order of elements in an array - reverse()

Remove the first element of an array - shift()

Select elements from an array - slice()

Sort an array (alphabetically ascending) - sort()

Sort numbers (numerically ascending) - sort()

Sort numbers (numerically descending) - sort()

Add an element at position 2 of an array - splice()

Convert an array to a string - toString()

Add a new element at the beginning of an array - unshift()