Skip to main content
2 of 2
added a note about how mine is a very bad example for the sake of demonstration.
Ryan Wood
  • 352
  • 2
  • 12

So, a lot of people are answering with pop(), but most of them don't seem to realize that's a destructive method.

var a = [1,2,3] a.pop() //3 //a is now [1,2] 

So, for a really silly, nondestructive method:

var a = [1,2,3] a[a.push(a.pop())-1] //3 

a push pop, like in the 90s :)

push appends a value to the end of an array, and returns the length of the result. so

d=[] d.push('life') //=> 1 d //=>['life'] 

pop returns the value of the last item of an array, prior to it removing that value at that index. so

c = [1,2,1] c.pop() //=> 1 c //=> [1,2] 

arrays are 0 indexed, so c.length => 3, c[c.length] => undefined (because you're looking for the 4th value if you do that(this level of depth is for any hapless newbs that end up here)).

Probably not the best, or even a good method for your application, what with traffic, churn, blah. but for traversing down an array, streaming it onto another, just being silly with inefficient methods, this. Totally this.

Ryan Wood
  • 352
  • 2
  • 12