JavaScript Arrays

Hi all,
Today we discuss one useful topic related to Javascript Array. An array is an object. This is mainly used to store a collection of items. Arrays become really very useful, you need to store large amounts of data of the same type.




You can access the items in an array by referring to its indexnumber and the index of the first element of any array is zero.

Creating an Array

You can create an array in JavaScript as given below.

var employee = ["Jose", "Varghese", "Thomas"];

Here, you are initializing your array as and when it is created with values “Jose”, “Varghese” and “Thomas”. The index of “Jose”, “Varghese” and “Thomas” is 0, 1 and 2 respectively. If you want to add more elements to the students array, you can do it like this:

employee[3] = "Mary";
employee[]4] = "Lilly";




You can also create an array using Array constructor like this:

var employee = ["Jose", "Varghese", "Thomas"];

OR

var employee = new Array();

employee[0] = "Jose";
 
employee[1] = "Varghese";
 
employee[2] = "Thomas";

Properties and Methods

The Array object has many methods and properties which help developers to handle arrays easily and efficiently.
You can get the value of a property by specifying arrayname.property and the output of a method by specifying arrayname.method().




  • length property: to know the number of elements in an array, you can use the length property.
  • prototype property: to add new properties and methods, you can use the prototype property.
  • reverse method : reverse the order of items in an array using reverse method.
  • sort method : sort the items in an array using sort method.



  • pop method : remove the last item of an array using pop method.
  • shift method : remove the first item of an array using shift method.

	

	 Javascript Arrays!!!

	

	

	

	

	

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

Leave a Reply

Your email address will not be published. Required fields are marked *