-1

Possible Duplicate:
What does "var FOO = FOO || {}" mean in Javascript?

I don't understand that :

var gapi = window.gapi||{};

Can you explain me? gapi is a bool?

Community
  • 1
  • 1
Christophe Debove
  • 6,088
  • 20
  • 73
  • 124

2 Answers2

4

It means if the variable gapi exists already, and its value does not evaluate to a boolean false one, assign that to the variable gapi otherwise assign a new object to it.

This practice is helpful to avoid overwriting of variables.

These are the JavaScript values that evaluate to boolean false:

  • false
  • undefined
  • null
  • 0
  • NaN
  • the empty string ("")
Shef
  • 44,808
  • 15
  • 79
  • 90
1

The logical or stops if the first symbol evaluates to something different from a truly value, like, false, undefined, null, '' or 0.. Otherwise it takes the second argument.

In this case, if gapi is not a global object defined in window, it assigns to gapi the new empty object {}.

user278064
  • 9,982
  • 1
  • 33
  • 46
  • 1
    No, `||` returns the first operand's value if it is truthy, otherwise it returns the second's. So please note that the second operand will be returned if the first operand has any of the falsy values `false`, `null`, `0` or an empty string (all of which are diferent to `undefined`). – nnnnnn Sep 10 '11 at 13:16
  • @nnnnnn: Yes, you're right. I have not properly expressed. – user278064 Sep 10 '11 at 13:36