SiteMap  | About    

JavaScript Arrays

An array, is a data structure consisting of a collection of elements, each identified by at least one array index or key. It is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

There are 3 ways to construct array in JavaScript

  1. The array literal, which uses square brackets.
  2. By creating instance of Array directly (using new keyword).
  3. The array constructor, which uses the new keyword.

Using Array Literal



By creating instance of Array directly

Using Array Constructor

Array Element Type

Array elements can be any type, including number, string, Boolean , null, undefined, object, function, regular expression and other arrays.

Access the Elements of an Array

In order to access the elements inside an array, you have to use square brackets and array index . JavaScript arrays begin at 0, so the first element will always be inside [0].



The length Property

Arrays in JavaScript are zero-based . This means that JavaScript starts counting from zero when it indexes an array. If a JavaScript array has five elements, then that array's "length" property will have a value of 5 (five). But , if a JavaScript array has five elements, the last element has an index of 4. This is because, JavaScript arrays are zero-based. The length property of an object which is an instance of type Array sets or returns the number of elements in that array.

When you run the above program, it will return 5.

Looping Array Elements

you can use "for" for Iterating over JavaScript arrays



Another way you can use for loop...


Add to the end of an Array

The push() method will add an element to the end of an array, while its twin function, the pop() method, will remove an element from the end of the array.

push() method


Remove from the end of an Array

pop() method


Add item(s) to the front of an Array

Using JavaScript Array unshift() Method to add the item in front of an Array.


Remove item(s) from the front of an Array


Modifying Items in Arrays

Array elements are accessed using their index number . Using this index number you can modify an item in an array.


Find the index of an item in the Array

JavaScript indexOf() method returns the index of the first occurrence of a value in an array.

The above program return 2.

How do I check if an array includes an object in JavaScript?

You can use the following code for find if an array contains a specific string in JavaScript.

The above code retuen "false", because "Friday" is not contains the array "days".


Javascript Array.sort() method sort the elements in an Array either Alphabetic or Numeric, also you can sort ascending or descending. The default sort order is alphabetic and ascending .


Arrays Undefined Values

Arrays are automatically growing and dynamically sized to accommodate any type of data that is added to them. So, when you add or remove elements from an array, the length of the array will change as needed. When you declare an Array with constructor , each slot will be set to "undefined".


Multidimensional arrays

A JavaScript multidimensional array is an array of arrays, or, in other words, an array whose elements consist of arrays.

Above program return 5.

JavaScript toString()

The toString() method returns a string representation of an object. (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.