Arrays in JavaScript

Introduction

 
An array is declared using square-bracketed notation as in the following:
  1. var names = ["Abhi""Ayush""Abhijeet"
To get an element put its index in square brackets (the first index is 0) as in the following:
  1. var names = ["Abhi""Ayush""Abhijeet"]  
  2. alert(names[0])  
  3. alert(names[1])  
  4. alert(names[2]) 
We can also retrieve its length as in the following:
  1. var names = ["Abhi""Ayush""Abhijeet"]  
  2. alert(names.length) 
Methods pop and push
 
There is a method pop that removes the last item and returns it.
 
Here we will see how the “abhijeet” is being popped off.
  1. var names = ["Abhi""Ayush""Abhijeet"]  
  2. alert("I remove " + names.pop())  
  3. // Now we have ["Abhi","Ayush"]  
  4. alert("Now length is: " + names.length) // Abhijeet is removed 
There is another method push that appends an element to the array.
 
Let’s say we’ve forgotten Bittoo as in the following:
  1. var names = ["Abhi""Ayush"]  
  2. names.push("Bittoo");  
  3. // now got ["Abhi", "Ayush", "Bittoo"]  
  4. alert("Last element is:" + names[names.length - 1]) 
Iterating over an array
 
To iterate over elements, a loop over all indices is usually used as in the following:
  1. var names = ["Ab""Yogi""Abhi""Ayush""Bittoo"]  
  2. for (var i = 0; i < names.length; i++) {  
  3. alert(names[i])  
Multidimensional arrays
 
Here is an example:
  1. var multi = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]  
  2. alert(multi[1][1]) //it is central element 
join and split
  • The join method combines an array into a string using a specifeid separator as in the following:
    1. var names = ["Ab""Abhi""Ayush""Bittoo"];  
    2. var s = names.join(', ');  
    3. alert(s); 
  • The split method is the reverse of join:
    1. var names = "Abhi,Ayush,Bittoo";  
    2. var ar = names.split(',');  
    3. // ar is ["Abhi", "Ayush", "Bittoo"]  
    4. alert(ar[0]); 
Removing from an array
 
Here is an example:
  1. var ar = ["Hello""Most""Welcome"]  
  2. delete ar[1]  
  3. // now ar = ["Hello", undefined, "Welcome"]  
  4. alert(ar[1]) // undefined 
A delete operator removes a key-value pair. Basically, because an array is just a hash, the slot becomes undefined.
 
We need to remove an item without leaving holes between indexes. For this, there is another method known as a splice.
 
Method splice
 
Syntax
  1. ar.splice(index, deleteCount[, elem1,..., elemN]) 
It can delete elements and replace them.
 
Here is an example:
  1. var ar = ["Hello""Most""Welcome"]  
  2. ar.splice(1, 1) // remove 1st element starting at index 1  
  3. alert(ar.join(',')) // ["Hello", "Welcome"] (1st element removed) 
anotherher example.
  1. Splice is able to insert elements, just set the deleteCount to 0.  
  2. var ar = ["Hello""Most""Welcome"];  
  3. // from 2nd position  
  4. // delete 0  
  5. // and insert "Hi", "Buddy"  
  6. ar.splice(2, 0, "Hi""Buddy")  
  7. alert(ar) // "Hello", "Most", "Hi", "Buddy", "Welcome" 
Method slice
 
We can also extract a portion of an array using slice(begin, end).
 
This method does not modify the array, it just copies a slice of it.
 
Example
  1. var ar1 = ["Hello""Most""Welcome"];  
  2. var ar2 = ar1.slice(0, 2) // take 2 elements starting at 0  
  3. alert(ar2.join(', ')) // "Hello, Most" 
Method reverse
 
Suppose I want the last part of a domain, like “com” from “www.abhijeet.com”.
 
Example
  1. var domname = "www.abhijeet.com"  
  2. var l = domname.split('.').reverse()[0]  
  3. alert(l) 


Similar Articles