JavaScript Array

JavaScript Array is section 10 of Beginner’s Guide to JavaScript.

An array is a special variable, which can hold more than one value at a time. If you have a list of items, for example a list of movie names, storing them in single variables could look like this.

var movie1 = “Veer Zaara”;

var movie2 = “Lucky”;

var movie3 = “Fanaa”;

But this looks disgusting. What if you want to loop through the movies and find a specific one? And what if you had not 3 movies, but 300?

The solution is an array! An array can hold many values under a single name, and you can access the values by referring to an index number. We describe array as something used to store multiple values in a single variable. So we can represent the above using an array as following two ways.

var movies = [“Veer Zaara”,”Lucky”,”Fanaa”];

Spaces and line breaks are not important. We can write the above as follows and it increases the readability.

var movies = [

“Veer Zaara”,




The other way is using the new keyword.

var movies = new Array(“Veer Zaara”,”Lucky”,”Fanaa”);

The two examples above do exactly the same. There is no need to use new Array(). For simplicity, readability and execution speed, use the first one array literal method.

To access a specif item in the array, we use index of the item. array index start from the 0. So the index of the item1 is 0.

var my_movie = movies[0];

You can also change the item of a specific index or add an item to a new index. But this is not the recommended way to add an item because it will create unnecessary holes in the array.

movies[0] = “Titanic”;

In the above few examples we used only strings as the items. But you can also use any type of items on an array. Even a mix of types.

myArray[0] =;
myArray[1] = myFunction;
myArray[2] = myCars;

Arrays are a special type of objects. The typeof operator in JavaScript returns “object” for arrays.

The length Property

The length property of an array returns the length of an array. Or else we can say number of elements in the array. The length property is always one more than the highest array index.

var movies = new Array(“Veer Zaara”,”Lucky”,”Fanaa”);

movies.length           // the length is 3

Adding Array Elements

The easiest way to add a new element to an array is to use the length property.

movies[movies.length] = “Doom”;

Difference Between Arrays and Objects

In JavaScript, arrays use numbered indexes.

In JavaScript, objects use named indexes.

Objects use names to access its “members”. Look at the following.  Movie object has the properties like the name, director and the released year. Also we can include methods for this as properties. All movies have the same properties, but their values and method behavior are different.

var movie = {


director:”James Cameron”,


getTitle = function(){

return “The movie “” was directed by “+this.director+” in year “+this.year;



In this example, returns Titanic.

JavaScript does not support associative arrays which have named indexes. Therefor you should use objects when you want the element names to be strings (text). And you should use arrays when you want the element names to be numbers.


What’s Next?

Learn what includes in JavaScript JSON

Written by Sandeep Likhar

Sandeep Likhar is from India, where he is a blogger, eBook designer, and founder of LetsDnD. He has 6 years of experience in the industry as a Digital Publishing Expert and eBook Converter, providing services to authors, publishers, and distributors worldwide. He is proficient in converting books into various formats, such as HTML, epub, mobi, word, PDF, including all major online platforms like iTunes, Kobo, Kindle, CreateSpace, B&N, Smashwords, and more.

Comment Below