JavaScript Array Object
the purpose of an array object is to use a separate variable name to store a series of values.
Online Examples
create an array, assign a value to it:
Example
var mycars = new Array(); mycars[0] = "Saab"; mycars[1] = "Volvo"; mycars[2] = "BMW";
you can find more examples at the bottom of the page.
What is an array?
An array object is a single variable name that is used to store a series of values.
If you have a set of data (e.g. car name), there are separate variables as follows:
var car1="Saab";
var car2="Volvo";
var car3="BMW";
However, if you want to find out a certain car from it? and not 3, but 300? it will not be an easy task!
The best way to do this is to use arrays.
Arrays can store all values with one variable name, and any one value can be accessed with a variable name.
Each element in the array has its own ID so that it can be easily accessed.
Create an array
There are three ways to create an array.
The following code defines an array object named myCars:
1: Conventional method:
var myCars=new Array();
myCars[0]="Saab";
myCars[1]="Volvo";
myCars[2]="BMW";
2: Concise way:
var myCars=new Array("Saab","Volvo","BMW");
3: Literally:
var myCars=["Saab","Volvo","BMW"];
Access the array
by specifying an array name and an index number, you can access a specific element.
The following example can access the first value of the myCars array:
var name=myCars[0];
The following example modifies the first element of the array myCars:
myCars[0]="Opel";
![]() | [0] is the first element of the array. [1] is the second element of the array. |
---|
You can have different objects in an array
All JavaScript variables are objects. Array elements are objects. Functions are objects.
therefore, you can have different variable types in the array.
you can include object elements, functions, arrays in an array:
myArray[0]=Date.now;
myArray[1]=myFunction;
myArray[2]=myCars;
Array methods and properties
use array objects to predefine properties and methods:
var x=myCars.length // the number of elements in myCars
var y=myCars.indexOf("Volvo") // The index value of the "Volvo" value
Complete array object references
You can refer to the complete reference manual for all the properties and methods of arrays on this site.
The reference manual contains descriptions of all properties and methods (more examples).
Complete array object reference manual
Create a new method
The prototype is a JavaScript global constructor. It can build properties and methods of new Javascript objects.
Example: create a new method
Array.prototype.myUcase=function(){ for (i=0;i<this.length;i++){ this[i]=this[i].toUpperCase(); } }
The above example creates a new array method for converting array lowercase characters to uppercase characters.
More Examples
Compose a string with the elements of an array - join()
Delete the last element of the array - pop()
Add a new element at the end of the array - push()
Reverses the order of the elements in an array - reverse()
Delete the first element of the array - shift()
Select elements from an array - slice()
Array sort (in ascending alphabetical order) - sort()
Sort numbers (in ascending numeric order) - sort()
Sort numbers (in descending numerical order) - sort()
Add an element at position 2 of the array - splice()