JavaScript- array

How to declare an array in JavaScript?

In this article we going to learn, How to declare an array in JavaScript. Before this let’s understand what is an array.

In JavaScript, An array is a special variable used to store multiple values at a time. An array is used when we want to store list of values under the single variable and we can access the values by referring to an index number. Each value is associated with a numeric index starting with 0. An array can store different values like Numbers, String and Boolean.

JavaScript Array

Declaration of an Array

An array in JavaScript can declare in two ways.

  • Array Literal : Creating an array is easy with using an array literal. it takes two square brackets and array values separated by a comma.

For example:

var fruits =["apple", "mango",........, "kiwi"];
  • Array Constructor : In this Method we can initialize an array by using new keyword.

For example:

//Initializing while declaring and creating an array  having value apple, mango, banana.//

var fruits = new Array("apple","mango","banana");

//Create an array of 3 undefined values//

var fruits = new Array(3);

//Create an array value mango//

var fruits = new Array("mango");

Access the Elements of an Array

We can Access an array by using it’s index number. For example: We have array named as fruits [“apple”, “mango”, “orange”] and we want to access “mango”. As we know, array index in starting from “0”. So we use index number 1 As, fruits[1];

<!DOCTYPE html>

<h2>JavaScript Arrays</h2>

<p>JavaScript array elements are accessed using numeric indexes (starting from 0).</p>

<p id="demo"></p>

var fruits = ["apple", "mango", "orange"];
document.getElementById("demo").innerHTML = fruits[1];


As a result:

JavaScript arrays implemented:

For the implementation of array we use various operations like POP () and Push() to perform actions at the end of an array. Pop is used to Delete an element from an array and Push s used to add the element in the array.

Array Function

  • Pop()
  • Push()
  • InsertAt()
  • deleteAt()
  • getElementAtIn()


By using Push() method we can easily Add a new element at the end of an array.


Array.push(element1, element2,......);

For example:

var fruits = ["Mango","Apple","Banana"];


With this array method we can delete any of the Elements from the array


Array.pop(element1, element2,......);

For example:

var fruits = ["Mango","Apple","Banana"];


With this array method, we can insert an element to the given array index. This method accepts two parameters called as index and item. The index is the number denoting the place where the data to be inserted and the item is a value which is to be inserted at the index.

This function is used to insert an element at given index.

insertAt(item, index) 
	for(let i=this.length;i>=index;i--) 


With this array method, we can delete an element to the given array index.

	for(let i = index; i < this.length - 1; i++) 
       {[i] =[i+1]; 


It return the element at givien index.

getElementAtIndex(index) { 

Recommended Posts:

How are java objects stored in memory?

jQuery Introduction


One thought on “How to declare an array in JavaScript?

Leave a Reply

Your email address will not be published. Required fields are marked *