Javascript Array

Array is used to store more than one value into single variable.


var arr=["John","Rom","Mathew"];

How to create an Array?


var item=[item1, item2,item3......itemn];

Example:-


var users=["John","Rom","Mathew"];

Creating a Array through new keyword


var users= new Array('John','Rom','Mathew');

First element of Array position is zero.


var users= new Array('John','Rom','Mathew');
console.log(users[0]) //Output:- John

Add element into the Array

push() method:- this method is used to store element into the Array.

Example:-


users=["John","Rom","Mathew"];
users.push("Clerk");
console.log(users);  //Output:- ["John", "Rom", "Mathew", "Clerk"]

Try it Yourself


 numbers=[10,20,30];
 numbers.push(40);
 console.log(numbers) //Output:- [10,20,30,40];

Note:- push() method is used to add element after the last element of the Array.

unshift() method:- this method is also used to add element before the first element of the Array.


users=["John","Rom","Mathew"];
users.unshift("Clerk");
console.log(users);  //Output:- ["Clerk", "John", "Rom", "Mathew"]

Try it Yourself


numbers=[10,20,30];
numbers.unshift(40);
console.log(numbers) //Output:- [40,10,20,30];

Remove element from the Array

pop() method:- this method is used to remove last element from the Array.

Example:-


users=["John","Rom","Mathew"];
users.pop();
console.log(users);  //Output:- ["John", "Rom"]

Try it Yourself


numbers=[10,20,30];
numbers.pop();
console.log(numbers) //Output:- [10,20];

shift() method:- this method is used to remove the first element from the Array.

Example:-


users=["John","Rom","Mathew"];
users.shift();
console.log(users);  //Output:- ["Rom","Mathew"];

Try it Yourself


 numbers=[10,20,30];
 numbers.shift();
 console.log(numbers) //Output:- [20,30];

Modify Array’s element value


users=["John","Rom","Mathew"];
console.log(users);  //Output:- ["John",Rom","Mathew"];

Example:- Now change the Rom to Andrew


 users=["John","Rom","Mathew"];
 users[1]="Andrew";
 console.log(users); // ["John","Andrew","Mathew"];

How to check variable is Array or not?

Array.isArray() function is used to check variable is Array or not.


users=["John","Rom","Mathew"];
var check_array=Array.isArray(users);
console.log(check_array);  //Output: true

Try it Yourself


  var name="John";
  var check_name=Array.isArray(name);
  console.log(check_name);  //Output: false

How to count the Array length?

length is used to count the total number of elements in the Array.


var arr=["John","Rom","Andrew","Smith"]
console.log(arr.length);  //4

Try it Yourself

Sorting Array

You can sort the Array element(which is numeric) Ascending and Descending order.

Ascending Order:-


  var arr=[10,5,7,3,12,15];
  var sort_arr=arr.sort(function(a,b){

	return a-b;
});
console.log(sort_arr);  
Output:- [3,5,7,10,12,15];

Descending Order:-


 var arr=[10,5,7,3,12,15];
  var sort_arr=arr.sort(function(a,b){

	return b-a;
});

console.log(sort_arr);  
Output:- [15,12,10,7,5,3];