JavaScript has objects and is object-oriented, but in an unusual way (more on that later).

Simple objects

Simple objects are like dictionaries or hashes in other languages. They support key-value storage and access to attributes. Here’s how to create a simple object.

js> obj = {}
[object Object]

js> typeof obj

We can store attribute values in a simple object (or any object in the doc:object-tree).

js> obj.s = 'hi'
js> obj.i = 10

We can get these values back again.

js> obj.s === 'hi'
js> obj.i == 10

Missing attributes

It’s not an error to ask for something that’s not there. We get undefined.

js> obj.dne === undefined

We still get undefined if we set the value to undefined.

js> obj.undef = undefined
js> obj.dne === undefined

You can use null to signal that there is a value, but that it is None.

js> obj.none = null
js> obj.none === null


You can use hasOwnProperty to help figure out why an object has an undefined attribute. It’s also useful when inspecting the doc::object-tree. Often, however, it’s better to write your code so you don’t need to do this (for example by using null).

js> obj.hasOwnProperty('dne')
js> obj.hasOwnProperty('undef')
js> obj.hasOwnProperty('none')

Object literals

You can create a simple object by placing key-value pairs in the curly braces.

js> someone = {
  >     'name': 'Joe Doe',
  >     'age': 43
  > }
[object Object]

js> someone.age

Take care not to put a trailing semicolon in the object literal. It will work in Firefox but not in Internet Explorer.


An array is a list of items. You can put anything is as a value for the list. Use square brackets to create an array.

js> array = []

Arrays expand to accomodate the data you store in them. You can even leave gaps.

js> array[0] = 'zero'
js> array[3] = 'three'

An array turned into a string consists of the string on its entries joined by commas.

js> array
js> array[1] === undefined

Array literals

As with simple objects, simply place the values between the square brackets, separated by commas.

js> seasons = ['spring', 'summer', 'autumn', 'winter']

As with simple objects, beware of trailing commas and missing entries. This will work in some browsers and not others.


JSON,stands for JavaScript Simple Object Notation. It is very much one of the best parts of JavaScript. It is the fat-free alternative to XML, and is widely used in AJAX (instead of XML). JSON objects are object and array literals constructed using only simple objects, arrays, strings, true, false and null.

If your code accepts JSON objects then it will be a lot easier to use it with AJAX or otherwise integrate it with other systems.

Many programming languages have JSON libraries. You don’t have to use JavaScript to use JSON.

To provide standards, there are rules on how to write a JSON objects. Many applications and programming languages will generate valid JSON for you. I find YAML a convenient way of authoring JSON data.