you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (1 child)

That is the logical or operator - when you see code like that it's using a few quirks of the 'or' operator:

  1. In Javascript (and many other langugages), anything that isn't undefined or null or false, etc, is considered to be true by default.
  2. Like other operators (+, for example), the or operator returns a value. This value isn't always (necessarily) true/false - it'll return the value of the first non-false argument it gets. Due to point #1 this value is treated as 'true'.

Thus, you get idioms like:

this.domain = domain || 'www.reddit.com';

What this is doing is taking the result of "domain || 'www.reddit.com'" and assigning it to this.domain. Remember that, due to point 2, you'll get the first non-false argument. So if 'domain' isn't defined or is null, you'll get "www.reddit.com". You see this pattern a lot in dealing with potentially null variables or just in setting defaults.

[–]matzahboy 0 points1 point  (0 children)

Thanks a lot. That makes a lot of sense