× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Arrays in Javascript: An Overview

Arrays is the one of the most used techniques in computer programming. In this article we will take a look of this technique and learn how to use it in Javascript.


An ordered collection of values is called an array. Each value has an index value which is its numeric position in array. The value of the array is called an element. In JavaScript, there is no type restriction. An array can have any type and can store different elements of different types. An array can store an object and even another array which allows a user to create complex data structure such as arrays of arrays and arrays of objects. JavaScript can have a maximum index value of 32 bit first index will be always zero and last will be 4294967294.In JavaScript user don’t need to fix the size of and array. Arrays are dynamic in nature in JavaScript and can grow and shrink as needed.

In JavaScript, Array.prototype defines a rich set of array manipulation methods. These methods are called generic because they work most of the array like objects, not only with the true arrays.

Creating Arrays

In JavaScript, we can create arrays using various methods. The simplest one is to create an array literal, which is a comma separated list of array elements within square brackets. For example:

Listing 1: Representing the Array declaration

var empty_arr = []; 				  // An empty array
var prime_num = [2, 3, 17, 11,13];       // An array with 5 prime numbers
var mix_val = [ 1.1, true, "a", ];	 // three different types and a trailing comma

An array can hold not only constant values but arbitrary expressions also:

var b = 1024;
var table = [b, b+1, b+2, b+3];

If a value is omitted from an array, then the omitted value is given a value undefined:

var arr1 = [1,,3]; 	//  the middle one undefined.
var arr2 = [,,]; 	 // both element are undefined.	

There is another way to create an array, use the Array() constructor. This constructor can be invoked in here ways:

  • Use the Array() constructor without any argument:
  • var b=new Array();

    An empty array with no element is created when this method is used.

  • Pass an argument which specifies the length of the array:
  • Var b=new Array(10);

    This method will create an array of specified length. This method is used when user knows in advance that how many elements are going to be needed. There is no value stored in array.

  • You can specify either two or more array elements or single non-numeric element for the array:
  • var c = new Array(6,7,8,9,10,  "testing…….");

    In this method, the constructor arguments are the elements of the array c.

Reading and Writing Array Elements

An [] operator is used to access the elements of an array. The brackets should be supplied with a non-negative integer value. This syntax can be used for both read and write operations on an array. Here, some examples are provided below:

Listing 2: Representing read and write operation on an array

var a = ["alok"]; 			  // create a one-element array
var value = a[0];			 // Read element 0
a[1] = 4; 				 // Write element 1
i = 2; 
a[i] = 6;			             // Write element 2
a[i + 1] = "hello"; 			// Write element 3
a[a[i]] = a[0]; 				// Read elements 0 and 2, write element 3 

Sparse Array

If an array has elements having non-contiguous indexes starting at zero, this type of array is called a sparse array. In arrays, the length property is generally represents the number of elements in array. In the case of sparse array, the value of length property is greater than the numbers of elements present in array. The implementation of an sparse array needs a slower, more memory efficient way than regular arrays. We can create an sparse array by assigning an array index larger than the current array length or we can use Array() constructor to create them:

Listing 3: Representing the examples of Sparse array

c = new Array(6); 		 // Array is empty but a.length is 6.
c = [];				// Array is empty and length is zero.
c[1000] = 0; 			// A single value is assigned but set the length to 1001

In general, we don’t use sparse array in our regular JavaScript development scenario. But a developer should be well versed with sparse arrays because it helps to understand the true nature of JavaScript.

Length of an Array

Length property is a very important feature of array in JavaScript. In array this property is responsible for specifying the number of elements in the array. It always return a value one more than the highest index in the array:

Listing 4: Representing the examples of Length property in array

[].length 				// it returns a zero, array has no element
['d','e','f'].length 		             // return 3 because highest index is 2 

In the case of an sparse array, the length property always return a value one more than its highest index number or we can say that it is impossible to have an element with greater or equal index to its length. But if in any case, if there is an element whose index is greater than or equal to the length, then that element will be deleted from that sparse array:

Listing 5: Representing the other examples of Length property in array

b = [6,7,8,9,10]; 		// 5-element array is created.
b.length = 3; 			// array has only [6,7,8] elements, last 2 have be deleted.
b.length = 0; 			// All elements have been deleted, array is empty.
b.length = 5; 			// Array length is set to 5 but have no element.  

push() and delete() Elements in an Array

Simplest method of inserting values in arrays is just assign values to new indexes:

Listing 6: Representing the examples of push() and delete() Elements in array

Example = [] 				// Create an empty element
Example[0] = "alok"; 			// And add elements to it.
Example[1] = "one";

A push() method is used to insert new elements at the end of the array:

b = []; 					// Create an empty array
b.push("zero") 			// A value is inserted at the end of the array
b.push("one", "two") 		// two more values are inserted

push() method is same as assigning the value to the a[a.length]. If you want to insert an element at the beginning of an array, then an unshift() method can be used, which shifts the existing elements to higher index.

To delete an array element, a delete operator is used:

d = [1,2,3]; 
delete d[1]; 			//   a now has no element at index 1
1 in d				//   false: no array index 1 is defined 
d.length 			//   3: delete does not affect array length

When use are using delete() method, you should keep one thing in your mind, deleting an element does not change the length property. It means no higher indexed value is shifted down to fill-in the gap created by the deleted element. Deleting is similar to assign undefined to that element. I this case, the array will become a sparse array.

A user can also delete elements from the end of an array by only setting the length property to the new desired length. Arrays have a pop() method also which is used to remove elements from the end and also returns the value of the deleted item. A shift() method is also available which shifts all elements to one lower than their current index.

Array reverse() method

The Array.reverse() method is used to reverse the order of the elements of an array and return the values in return order. This method does not create an array of rearranged elements, but it actually rearranged the existing order of that array and then returns the values. In the following example, a reverse() and join() method is used to produce the string “10,9,8”:

Listing 7: Representing the examples of reverse() method of array

var a = [8,9,10];
a.reverse().join() 		// =>  a is now [10,9,8] and returns “10,9,8”


We learned in this discussion about what an array is and where and how it can be used in JavaScript. In JavaScript, array are totally different when we compare them with the arrays of other languages such as C or Java. In these languages, arrays are strictly types data, use must have to declare the type of an array before its creation. Except this, an array in those languages cannot store multiple values of multiple data types. But in JavaScript, it is common to assign different type of values in array. There is no need to prior type declaration of the array. This documentation gives you an overview of JavaScript arrays.

Have total 6+ years of experience in developing enterprise applications using DOT.NET 3.5 and 4.0(C#, VB.NET, ADO.NET, ASP.NET),java, JQuery, JSON, LINQ,WCF, MVC3, MVC4, Silverlight, SQL Server, mobile applications and Oracle etc ...

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login