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?

0

3 Answers 3

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' }.

Sign up to request clarification or add additional context in comments.

2 Comments

It tests if THREE evaluates as true, not if it is defined.
@Quentin - Answer updated - better?
1

In code, it's like saying:

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

Or:

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

2 Comments

What if THREE was 0?
@Quentin Good point! Hope I got it right now(?) =)
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.