Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 276
rememberlenny Avatar asked Apr 29 '13 17:04

rememberlenny


1 Answers

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.

like image 184
nullability Avatar answered Oct 21 '22 04:10

nullability