Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ||{} mean in javascript?

I'm working on a project using Easel JS. Opened up the Easel file and the very first line of code confused me:

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

I know that createjs is invoked when you're setting up your canvas or, for example, creating a bitmap to add to the canvas. But I don't understand the syntax of this line - assign this.createjs or (what I guess is) a blank object to this.createjs?

like image 245
unaesthetic Avatar asked Dec 19 '22 05:12

unaesthetic


2 Answers

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

If this.createjs is not available/ any falsy value then you are assigning {} empty object to this.createjs.

It's more like,

var a, 
    b;

b = a || 5;

Since a is not having any value currently, 5 will be assigned to b.

like image 83
mohamedrias Avatar answered Jan 11 '23 23:01

mohamedrias


Correct. This ensures that if this.createjs doesn't already exist, an empty object is assigned to it. The || is an or operator - if this.createjs on the left-hand-side evaluates to falsy, it will assign the right-hand-side instead.

like image 34
Andrew Ferrier Avatar answered Jan 11 '23 23:01

Andrew Ferrier