Archive for May, 2009

Nifty Little Javascript Array Trick

This may well be common knowledge, but to me I’ve found this little trick immensely useful.

Because Javascript automatically resizes arrays when you add a new item, this nifty little syntax works great for building a new array or adding items on the end:

1: var ar = []; 2: for(var i = 0; i < 50; ;++) { 3: ar[ar.length] = "Some value " + i; 4: }

Obviously the push() and pop() methods work well too, but I find the above syntax slightly more readable since I don’t typically expect an array to have methods like push() and pop().


Continue Reading 2009/05/18 at 23:08 1 comment

My Most Recent Updates