What does || {} in javascript?

I am working on a project using Easel JS. Opened the Easel file, and the first line of code confused me:

this.createjs = this.createjs||{};

I know that createjs is called when you customize your canvas or, for example, create a bitmap to add to the canvas. But I do not understand the syntax of this line - assign this.createjs or (what I think) an empty this.createjs object?

+4
source share
4 answers
this.createjs = this.createjs||{};

If this.createjsunavailable / value falsy, then you assign {}an empty object this.createjs.

It looks more like

var a, 
    b;

b = a || 5;

Since it adoes not matter at present, it 5will be assigned b.

+6

. , this.createjs , . || - a - this.createjs falsy, .

+2
this.createjs = this.createjs||{};

If this.createjs is false, this.createjs will be the new empty object

You could replace it with

if (!this.createjs){
     this.createjs = {};
}
+2
source

||means or. In this context, means this.createjsequal if there is / not null / defined in this.createjsanother way{}

+1
source

All Articles