0

I've seen some code in the form of:

var vendorcode = vendorcode || {};

I've always thought that || was a logical operator. But this one is a real doozy.

Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
surfasb
  • 968
  • 1
  • 13
  • 31
  • See also [What does β€œvar FOO = FOO || {}” mean in Javascript?](http://stackoverflow.com/questions/6439579/what-does-var-foo-foo-mean-in-javascript) for why this commonly appears at the top of JS files. – apsillers Jun 15 '15 at 19:35

1 Answers1

9

This code assigns {} to vendorcode if vendorcode is false-y. Meaning it's undefined, false, 0, null, etc.

If vendorcode is not false-y it'll keep its value.

You can read it out loud as: "vendorcode equals vendorcode OR {}"

deadboy
  • 849
  • 2
  • 9
  • 28
  • I figure it had to do something with an assignment. I didn't realize || could be used with an assignment of a function. – surfasb Jun 15 '15 at 19:29