Busted Mug

A blog that documents solutions to the most frustrating problems that occur during development in technologies such as Java, XML, AJAX, SQL, CSS and others that make me want to throw my coffee mug against the cube wall.

Wednesday, December 06, 2006

JS Array hax: Push Pop Shift and Unshift

These incredibly useful array methods are often overlooked and under utilized. When used properly, if for some reason you need to change your code from appending an array to pre-pending it, you just change from push() to unshift(). It should take all of two seconds. Without using these functions you'll end up writing 10 lines or so that are SOOOOOO boring you'll want to barf. Here's a quick reference of what they do: push() - add element to the end of the array pop() - remove and return the element at the end shift() - remove and return the element at the beginning unshift() - add element to the beginning of the array pushing the others down 1 Shift and unshift seem to me to save the most time.


Post a Comment

<< Home