0

Possible Duplicate:
What does “options = options || {}” mean in Javascript?

I stumbled upon this line, and can't seem to figure out what it means

var G = G || {};

Any ideas?

Community
  • 1
  • 1
Loupax
  • 4,728
  • 6
  • 41
  • 68

3 Answers3

5

G = G, and if G does not exist, create it as an empty object.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
5

G is G or a new object if G is not defined "falsy".

kapa
  • 77,694
  • 21
  • 158
  • 175
Yuriy Zubarev
  • 2,821
  • 18
  • 24
3

If G is currently any "falsey" value, then the object literal will be assigned to G.

The "falsey" values are...

  • undefined
  • null
  • ''
  • NaN
  • false
  • 0

The operator being used is the logical OR operator.

The way it works is that it evaluates its left operand first. If that operand has a "truthy" value (any non-falsey value), it returns it, and doesn't evaluate (short-circuits) the second operand.

If the left operand was "falsey", then it returns its right operand, irrespective of its value.


Example where G is falsey...

//      v--- Evaluate G. The value of G is the "falsey" value undefined...
var G = G || {};
//            ^--- ...so evaluate and return the right hand operand.

Example where G is truthy...

G = 123;

//      v--- Evaluate G. The value of G is a "truthy" value 123...
var G = G || {};
//      ^--- ...so return 123, and do not evaluate the right hand operand.
  • That's what I guessed, but it makes no sense to me, because of the "var" part. I mean, the G variable is defined in the same line, what's the point of giving it the value of G||{}? Isn't it the same as var G = {}; ? – Loupax Feb 06 '12 at 19:54
  • @Loupax: Yes you're right, if there's no way that `G` could have already been initialized, then the `||` doesn't have much point. Sometimes people unnecessarily redeclare variables, or declare variables that are already declared as parameters, so maybe that's what's happening here. –  Feb 06 '12 at 19:57
  • ...note that `undefined` is the default value for declared variables. –  Feb 06 '12 at 20:00
  • Doesn't undefined || {} == false? Empty objects evaluate to true? That's something I didn't know... – Loupax Feb 06 '12 at 20:06
  • @Loupax: Any object will give a "truthy" evaluation. You should note that truthyness is a boolean evaluation, while `==` is not. `Boolean({}); // true` `Boolean(undefined); // false` –  Feb 06 '12 at 20:09
  • Now I'm more confused than before o_O I mean, I know what the line does, but how it works? Shouldn't var G = G || {} give G the value true instead of empty object? – Loupax Feb 06 '12 at 20:12
  • 1
    @Loupax: No, it evaluates the operand as "truthy" or "falsey", but it always returns the operand itself. So lets say the first operand is `{foo:'bar'}`, it evaluates its "truthyness", decides that it is indeed "truthy", and so it returns the object. So while it *considers* its truthyness, it never *coerces* the operand to a boolean. –  Feb 06 '12 at 20:15
  • And that explains everything! Thank you for your time! – Loupax Feb 06 '12 at 20:17
  • @Loupax: You're welcome. You should note that these operators may be combined, in which case, it works from left to right until it returns the first "truthy" operand found, or the last operand. [Here's a previous answer](http://stackoverflow.com/a/8578135/1106925) I gave that demonstrates the behavior when you have multiple `||` in a row. –  Feb 06 '12 at 20:22