In JavaScript Arrays are an object. You can create Array is JavaScript as following

 

image

An array element can be accessed using its index. For example if you want to read banana, you will read that as following,

 

image

Likely Array of other languages, JavaScript array index also start from zero. The other way to create JavaScript array is by using the new keyword.

 

image

You can read elements of array using for loop as following,

image

You will get following output. You will notice that value of the 5th element in array is undefined since we have not set the 5th element value.

 

image

Even though you have set the size of the array as five, you can dynamically increase size of the array. In below listing we are setting value of 10th element. After setting 10th element value size of the array has been increased from 5 to 10.

 

image

Since we have not set values from 5th element to 9th element, so those value will be undefined

 

image

Following are the most used functions of JavaScript Array.

 

image

Let us explore each functions one by one.

Push Function

Push inserts an element at the top of the array. Suppose we have an array

 

image

When you see the console , you will find array elements as following

 

image

Now let us push some elements in fruit array . We can push element in the array by calling push function and passing the elements to be inserted as input parameter. In following listing , we are inserting orange and apple in the array. First orange will be inserted then apple.

 

image

And as a output you will get array elements as below,

image

Pop Function

Pop function is used to remove top or last element of the array. From above array we can pop an element as given in below listing

 

image

You will find that banana has been removed from the array

image

unshift Function

unshift function inserts elements in the beginning of the array. Let us suppose we have an array

image  

You can insert elements at the beginning of array using unshift as following

image

You will find that orange , apple and grapes has been added to the fruits array . Now the first element of the array is grapes.

 

image

shift Function

shift function removes first element from the array. Let us consider this array

 

image

when we call shift function on array mango will be removed from the array

image

You will get array elements printed as following

image

In further post we will discuss more JavaScript Array functions.  I hope you find this post useful. Thanks for reading.

Advertisements

5 thoughts on “Arrays in JavaScript

  1. You are an angel for me. I have wasted my whole night in this problem. m newbie :(… thx for the help… Bookmarking it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s