Chris Nizzardini, Salt Lake City Utah, Web Developer Specializing in LAMP+Ajax Since 2006

My Blog

Here is my awesome blog.

More JavaScript Array Methods (push and join)

I found some other badly needed javascript methods today. Push (similiar to PHPs array_push function) and Join, which converts a simple array into a string. The Join method is great if you want to pass a dumb string to a server side language using an HTTP GET or POST.

Push

/* this can be defined as a global variable and then accessed by many different functions */
var myArr = new Array();
myArr.push("another element");

This simply adds another element to the end of the array.

Join

var myArr = new Array();
myArr[0] = "element 1";
myArr[1] = "element 2";
myArr[2] = "element 3";
var myStr = myArr.join(', ');

Now you have converted an array into a comma seperated string.

Leave a Reply