in JavaScripting

Object and Array literal in JavaScript

This post is mostly for my own reference as I seem to keep messing this up. :O

The basics

{} is an object literal

//this creates a new object
a = {};
//so does this
a = new Object();


[] is an array literal

//this creates a new array
a = [];
//so does this
a = new Array();


properties can be accessed via . and [] notation:

//these are the same
a.b = c;
a['b'] = c;


Object literals can be set using literal values or strings:

a = {
    //any character you can use for a variable name can be
    //used to instantiate an object without quotes
    b: c,
    //special characters need to be quoted
    "foo bar baz": "fizz buzz"