JavaScript Array
Global Objects - Array
Array :
Next object under the category of global objects in JavaScript is 'Array'.
Array in JavaScript is an object that allows storing of multiple values in a single variable.
Indexes in JavaScript array can be of type number or string.
If index is number, the index of first item inside an array will be zero '0'.
If index is a string, the array is referred as an associative array. These arrays have indexes as keys that store value with any valid JavaScript data type.
JavaScript arrays are allowed to contain elements of different types. Such an array is called as dense array.
JavaScript arrays have a number of useful properties and methods to access or modify the given array. Let us explore some of them.
Let us now see the methods of an Array.
Method Name |
Description |
Example |
push() |
Adds new element at last position in an array |
var myArray=["Android","iOS","Windows"];
myArray.push("Linux");
console.log("Updated array contents: "+myArray);
//Returns
Updated array contents: Android,iOS,Windows,Linux |
pop() |
Removes last element from given array |
var myArray=["Android","iOS","Windows","Linux"];
console.log("Items in array initially: "+myArray);
var poppedOutItem=myArray.pop();
console.log("Removed item: "+poppedOutItem);
//Returns
Items in array initially: Android,iOS,Windows,Linux
Removed element:Linux |
push() |
Adds new element at last position in an array |
var myArray=["Android","iOS","Windows"];
myArray.push("Linux");
console.log("Updated array contents: "+myArray);
//Returns
Updated array contents: Android,iOS,Windows,Linux |
splice() |
Adds new elements at specified position in an array
Syntax: splice(x,y,z)
x=index for new item
y=number of items to be removed, starting from index next to index of new item
z=item to be added |
var myArray=["Android","iOS","Windows"];
console.log("Items in array initially: "+myArray);
myArray.splice(1,0,"Linux");
console.log("Updated array: "+myArray);
//Returns
Items in array initially: Android,iOS,Windows
Updated array: Android, Linux, iOS, Windows |
concat() |
Joins two or more arrays and returns joined array. |
var myArray1 = ["Android","iOS"];
var myArray2 = ["Samsung", "Apple"];
console.log("Concatenated array: " +myArray1.concat(myArray2));
//Returns:
Concatenated array: Android, iOS, Samsung, Apple |
forEach() |
Iterates over array to access each indexed element inside an array. |
var myArray = ["Android", "iOS", "Windows"];
myArray.forEach( function( currentItem, index){
console.log("myArray has" + currentItem +"at index" + index);
})
//Returns
myArray has Android at index 0
myArray has iOS at index 1
myArray has Windows at index 2 |
Demo :
CODE/PROGRAM/EXAMPLE
var myArray = ["Android", "ios", "Windows"];
document.write(
"Length of the array is : " + myArray.length);
var myArray = ["Android", "ios", "Windows"];
myArray.push("Linux");
document.write("Updated array contents: " + myArray);
var myArray = ["Android", "ios", "Windows", "Linux"];
document.write("Original array items: " + myArray);
var poppedOutItem = myArray.pop();
document.write(
"Element that was removed: " + poppedOutItem);
var myArray = ["Android", "ios", "Windows"];
document.write("Original array items: " + myArray);
myArray.splice(1, 0, "Linux");
document.write("Updated array: " + myArray ;
var myArray1 = ["Android", "ios", "Windows"];
var myArray2 = ["Samsung", "Apple", "Nokia"];
document.write(
"Concatenated array is : " + myArray1.concat(myArray2));
var myArray = ["Android", "ios", "Windows"];
myArray.forEach(function(currentItem, index) {
document.write( "Array myArray has " + currentItem + " " + "at index" + " " + index );
});