2

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

What is the meaning of the || in the second argument?

   var obj = this;
   var settings = $.extend({
       param: 'defaultValue'
   }, options || {});

Also would be nice if anyone knows how to search that character("|") here or in google! Thank you

Community
  • 1
  • 1
  • http://stackoverflow.com/questions/2100758/javascript-or-variable-assignment-explanation – lc. Jan 22 '13 at 18:38
  • To answer the last part of your question, you would want to search for "javascript logical or in assignment statement" or something similar. – jbabey Jan 22 '13 at 18:56

5 Answers5

9

That would be the logical OR. The statement will return the first truth-y value it finds.

In this case, if options is null (or any other value that isn't truth-y) it will evaluate to false. The || will then return the empty object.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 1
    Here is the [**comprehensive list of what is falsy**](http://es5.github.com/#x9.2). – phant0m Jan 22 '13 at 18:39
  • 1
    That isn't a logical OR though, is it? Wouldn't the result of a logical OR return a boolean, not one of the values? This is more like a null-coalesce IMO. – Stefan H Jan 22 '13 at 18:40
  • @StefanH No, it doesn't always return a boolean, it returns one of its operands. – phant0m Jan 22 '13 at 18:41
  • @StefanH It's called OR because it's what it does when fed boolean values. – John Dvorak Jan 22 '13 at 18:41
  • 1
    @StefanH - Oddly enough, no. Take a look at the documentation at MDN - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Logical_Operators – Justin Niessner Jan 22 '13 at 18:42
  • @phant0m I realize that in the situation above it doesn't resolve to a boolean. That was kind of my point. – Stefan H Jan 22 '13 at 18:43
  • @JustinNiessner That makes sense - that is an aspect of Javascript's logical operators that I wasn't quite aware of. Thanks! – Stefan H Jan 22 '13 at 18:43
  • @StefanH I only wanted to confirm your suspicions. – phant0m Jan 22 '13 at 18:46
1

That is some kind of fallback value or default value. So if the object is null or false the second value is used.

rekire
  • 47,260
  • 30
  • 167
  • 264
1

More importantly in that scenario, if options is not defined then an empty object {} is passed as an argument. Its kind of a side-effect use case of the logical OR operator. More specifically, it uses short circuiting. For example in the below case

a || b

if a is true then b never gets executed, but if a is false then b gets executed. Hence in the example you have shown, if options is not defined and thus false, then {} gets executed and thus passed as a parameter.

dchhetri
  • 6,926
  • 4
  • 43
  • 56
0

|| = or

As in the comparison operator.

DrCorduroy
  • 92
  • 9
0

|| = "OR". Example:

alert(false || false || false || "I'm valid!"); // alerts "I'm valid"

In your question, the example above demonstrates that the function requires an object for it's options. In this case, if the local variable "options" is not available, then just pass an empty object. Later, in the function that's being called, it's probably setting default values in that new object.

Shaz
  • 15,637
  • 3
  • 41
  • 59