How to Print Array in JavaScript

This tutorial will helps you to find how to declare array and print an array in JavaScript.

How to Print Array in JavaScript

In this section you will learn about Array in JavaScript. Array are important part of all programming languages. Array in JavaScript is a variable that can hold more than one value. Array is a collection of data item. Each element of array can store its own data. Element of array can be added and removed at any time and its value can also be changed. The important feature of array is that it can hold different type of data.

You can create an Array in different ways as follows :

1.var mycity=new Array(); 

2. var mycity = new Array("Delhi", "Agra", "Jaipur");

3. var mycity = ["Delhi", "Agra", "Jaipur"];

Access an Array :

By index you can access the array. To access the array you can write it as follows:

var a=myCity[0];

This statement access the first element of array. If you want to change the value of array then


There are some methods of array that can use in array like length() can find the length of array.

var b=mycity.length // Number of element in the array
var c=mycity.indexOf["Agra"]//return the index of Agra

Now, here is the example how to declare and access array.

<!DOCTYPE html>
<script>function arraycreation()
var mycity = new Array(5);
for(var i=0;i<mycity.length;i++){
document.write("<b>mycity["+i+"] is </b>=>"+mycity[i]+"<br>");
<body bgcolor="EECFE#"><h1>Java Script Array example</h1><p><h2>Click to print the array element</h2></p><p><input type="button" value="Click" onclick="arraycreation()"> </input></p></p></body>

When you execute this program output will be as follows:

When user click on the button , it will display the content of an array.

Download Source Code

Share on Google+Share on Google+

How to Print Array in JavaScript

Posted on: July 6, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: How to Print Array in JavaScript  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image