Skip to content

Instantly share code, notes, and snippets.

@majgis
Created February 21, 2013 17:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save majgis/5006311 to your computer and use it in GitHub Desktop.
Save majgis/5006311 to your computer and use it in GitHub Desktop.
Javascript while loop to deplete an array from either direction.
var x = [1,2,3]
//back to front
while(x.length){
var value = x.pop();
console.log(value);
}
//array is now empty, lets replace it
var x = [1,2,3]
//front to back
while(x.length){
var value = x.shift();
console.log(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment