What does "var app = app || {};" do?

backbone.js, javascript

Solution

It will define the variable `app` to an empty object if it is not already defined.

This works because being undefined evaluates to `false` in Javascript.

If it is defined, it may still be re-defined as an empty object if it has a value which evalutes to `false`, such as an empty string.

Problem

Im looking at some Backbone.js examples and they have `var app = app || {};` at the top of all .js files. I understand the literal meaning of this, but what does it do in reference to everything else? Edit: you guys are really really fast.

Original source

Related problems