Array in Javascript

Array is a variable that is used to hold more than one value in a single variable. That is we can hold a list of data inside an array.

How to declare an array in javascript ?

We can declare an array in two ways. these are the ways we can achieve it.

Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayName2 = ["data 1", "data 1", "data 3"];

How to add elements to array in javascript ?

Javascript array has a method push, the method we can use to add new elements to the array. when we use push the element will be added at the end of the array.
Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayLength=arrayName1.length; // output will be 3
arrayName1.push("data 4");
var arrayNewLength=arrayName1.length; // output will be 4

How to delete an elements from array in javascript?

Javascript array has method named pop , it help us to delete an element from an array. when we use pop the elements at the end will be deleted.

Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayLength=arrayName1.length; // output will be 3
arrayName1.pop();
var arrayNewLength=arrayName1.length; // output will be 2

How to find number of elements in an array in javascript ?

Javascript array has a property called length , the length property help us to find the number of elements exist in an array.
Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayLength=arrayName1.length; // output will be 3

Leave a Reply

%d bloggers like this: