Javascript array/object shortcuts

I always forget the syntax to make a new object or array in Javascript.

var my_array = new Array();
my_array.push(1);
my_array.push(2);
my_array.push(3);

Or you can simplify the whole process:

var my_array = [1,2,3];

This is the syntax to make a new Object (hash) in Javascript:

var my_obj = new Object();
my_obj['foo'] = 'bar';
my_obj['apple'] = 'red';

But can be shortened this way:

var my_obj = { 'foo':'bar' , 'apple':'red' }

Just remember that arrays are initialized with [] and objects are initialized with {}. This is the same syntax as Perl's array and hash references.



Note: Replies will be formatted with PHP Markdown Extra syntax.

Name: Email (Not Required):
 
Logged IP: 3.135.219.166
To prevent spam please submit by clicking the kitten: