0

I'm investigating Three.js at the moment and have come across this variable declaration at the top of the main source file:

var THREE = THREE || { REVISION: '52' };

I'm just wondering what the OR (||) is doing in there - what is the function of it?

unfrev
  • 737
  • 1
  • 5
  • 19

3 Answers3

4

The above means:

If the value of THREE evaluates to true, assign the value of THREE to the THREE variable, otherwise initialize it to the object { REVISION: '52' }.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

In code, it's like saying:

var THREE;
if (THREE) {
    THREE = { REVISION: '52' };
}
else {
    THREE = THREE;
}

Or:

var THREE = (THREE) ? { REVISION: '52' } : THREE;
Mario S
  • 11,715
  • 24
  • 39
  • 47
0

lazy instantiation. If the variable is declared already, then assign a value to it.

Ahmad
  • 12,336
  • 6
  • 48
  • 88