Month: October 2010

JavaScript Array functions push and pop

Hi All,

These are the some javascript functions which come in handy while writing javascript code.

push() and pop()
This comes in handy when you want to store some elements to array as you go on in the code. Think of a scenario where you need to collect some data in a big module and at the end i will show up the user that x number of elements are there and those elements are … etc. push() is a very efficient method in this case


var myDScollection = []; // Yes this is the array don't worry, and also JSLint says this is the best.

//Pushing a string
myDScollection.push('hey there');

//pushing a variable value
var myLoopVar1 = 10;
myDScollection.push(myLoopVar1);

So this array can be used to get the count or loop through each of the element of myDScollection

for (i = 0; i < myDScollection.length; i++) {
    document.write(myDScollection[i] + '
')
}

Similary you can pop the contents of the array using myDScollection.pop()

Below is the link for playing with this stuff. Yes! I am right, here onwards there will be no demo files to download or show, you will have the direct access to my code what ever i am trying out for this blog. Enjoy 🙂

Play