JavaScript tutorial-Array object


JavaScript Array object

In the purpose of helping you to work with arrays, JavaScript provides you Array object that contains useful properties and methods. To create an Array object, you need to use the new keyword.

Example:

var ArrObj=new Array();

You can assign values to the array(when it is created) by putting those values in brackets separated by comma.

Example:

var ArrPro=new Array("P001","P002","P003");

Alternatively, you can assign the values to the Array object after it is created by using the indices of the Array object.

Example:

var ArrStu=new Array();

ArrStu[0]="Stu001";

ArrStu[1]="Stu002";

The way to access the array object is the same as you leant in the Array section of this JavaScript tutorial. Now we move to the useful properties and methods of the array object.

length property

The length property can be used to count the number of elements in the array object. It also help us easily access the last element of the array object. The index of the last element is equal to the length subtracted by one.

Example:

<html>
<head>
<title>Using length property</title>

<script language="JavaScript" type="text/javascript">
var ArrStu=new Array();
ArrStu[0]="Stu001";
ArrStu[1]="Stu002";
document.write("The number of elements is "+ArrStu.length+"<br>");
document.write("The last element is "+ArrStu[ArrStu.length-1]);
</script>

</head>
<body>
</body>
</html>

sort method

If you have an array object to store students' names and you want to sort the names in alphabet or numerical order, you can use the sort() method. The sort() method is case-sensitive. This means that Dara will come first then dara comes after.

Example:

<html>
<head>
<title>Using sort() method</title>

<script language="JavaScript" type="text/javascript">
var ArrStu=new Array();
ArrStu[0]="Sambath Chan";
ArrStu[1]="Sak Vibol";
ArrStu.sort();
var i;
for(i=0;i<ArrStu.length;i++)
document.write(ArrStu[i]+"<br>");

</script>

</head>
<body>
</body>
</html>

concatmethod

The concat() method can be used to combine all elements of an array object with all elements of another array object in to a single array object. In the example below, we have two array objects--ArrID and ArrName. Then the concat() method is used to combine the two array objects in to a single array object called ArrStu.

Example:

<html>
<head>
<title>Using concat() method</title>

<script language="JavaScript" type="text/javascript">
var ArrID=new Array("Stu001","Stu002");
var ArrName=new Array("Dara Chan","Sok San");
var ArrStu=ArrID.concat(ArrName);
var i;
for(i=0;i<ArrStu.length;i++)
document.write(ArrStu[i]+"<br>");

</script>

</head>
<body>
</body>
</html>

reverse method

By using the reverse() method, you can rearrange the elements of an array object in reversal order. This means that the first element is swapped with the last and the second first is swapped with the second last and so on.

Example:

<html>
<head>
<title>Using reverse() method</title>

<script language="JavaScript" type="text/javascript">

var ArrProduct=new Array("Anchor","Angkor","Cocacola");
ArrProduct.reverse();
var i;
for(i=0;i<ArrProduct.length;i++)
document.write(ArrProduct[i]+"<br>");

</script>

</head>
<body>
</body>
</html>

slice method

The slice(startInde, endIndex) enables you to copy part of an array object. The copy process starts from the startIndex until the endIndex subtracted by one. In the example below, we take only the first and the second elements of the array object called ArrProduct. The taken elements are placed in the Product array.

Example:

<html>
<head>
<title>Using slice() method</title>

<script language="JavaScript" type="text/javascript">

var ArrProduct=new Array("Anchor","Angkor","Cocacola");
var Product=ArrProduct.slice(0,2);
var i;
for(i=0;i<Product.length;i++)
document.write(Product[i]+"<br>");

</script>

</head>
<body>
</body>
</html>

join method

Sometimes, you don't want to combine multiple array objects together but want to combine all elements of an array object in to a single string value. This can be done by using the join() method. In the example below we use the join() method to combine all elements of the array object called ArrName together.

Example:

<html>
<head>
<title>Using join() method</title>

<script language="JavaScript" type="text/javascript">

var ArrName=new Array("Dara Chan","Sok San");
var Names=ArrName.join();
document.write(Names);

</script>

</head>
<body>
</body>
</html>

By default, the separator comma (,) is inserted between the elements of the array object in the result string value. If you want the separator different from the comma you can specify your separator by supplying it as the argument of the join() method. In the next example we separate the elements of the array object called ArrName in the result string value with the minus sign(-).

Example:

<html>
<head>
<title>Using length property</title>

<script>

var ArrName=new Array("Dara Chan","Sok San");
var Names=ArrName.join("-");
document.write(Names);

</script>

</head>
<body>
</body>
</html>




Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.