21

I am looking at these lines of code from here:

    if (callback)
        callback(sig || graph);

I have never see vertical "or" bars in a javascript method call. What do they mean? Do they pass the "true" parameter (i.e. sig or graph)? Do they pass the defined parameter? I have never seen that syntax before.

bernie2436
  • 22,841
  • 49
  • 151
  • 244

6 Answers6

33

This is the logical OR operator in JS (and most other languages). It is defined in the spec at 11.11. As noted in the spec, expressions on either side will be evaluated first and the logical OR is left-to-right associative. Note that evaluation of the operands follows standard ToBoolean semantics from section 9.2, so [null, undefined, 0, ''] all count as falsy.

Unlike most languages, JS returns the left operand if it is truthy or the right operand otherwise. This behavior has been covered before in a number of SO questions, but is worth noting as most languages simply return true or false. This behavior is often used to provide default values to otherwise undefined variables.

Community
  • 1
  • 1
ssube
  • 47,010
  • 7
  • 103
  • 140
  • 2
    It doesn't return `false` if both are falsy; it returns the value of the right-hand side, whatever it may be (so, `0` maybe, or `""`). – Pointy May 07 '15 at 18:02
  • @Pointy my mistake, fixed. – ssube May 07 '15 at 18:03
  • "Unlike most languages, JS returns the left operand if it is truthy or the right operand otherwise." is a little broad. This approach is fairly common among scripting languages with a concept of "truthiness", e.g. Python and Perl. Even Ruby has this behavior, though it's not as useful (because almost everything in Ruby is truthy, so the left-hand side must be coerced to boolean for the test, which means this only works for defaulting from `false` or `nil` to another value, not from arbitrary "falsy" objects like 0 values, empty sequences, etc.). – ShadowRanger Feb 22 '18 at 01:51
12

The Logical OR operator (||) is an operator that returns its first or second operand depending on whether the first is truthy. A "truthy" value means anything other than 0, undefined, null, "", or false.

This operator uses short-circuiting, meaning if the first expression is truthy, then the second expression is not evaluated and the first operand is returned immediately. This is akin to the Logical AND operator (&&), which does the opposite: if the first operand is falsey, it returns it, otherwise it returns the second expression.

David G
  • 94,763
  • 41
  • 167
  • 253
3

It means 'or' (http://www.w3schools.com/js/js_comparisons.asp) So if(sig OR graph)

BE CAREFUL you can 'short circuit' your code using this. example :

If (foo || foo2)

if foo is true, then JavaScript wont even test foo2 at all, it just skips it.

Ian Wise
  • 706
  • 2
  • 10
  • 31
1

It passes whichever evaluates as true, or sig if both are true.

howderek
  • 2,224
  • 14
  • 23
0

The double pipe (||) represents OR in JS. In simple words, either this or that is True. It requires any of the sides true to get a True result.

For example:

var x = 8;

var y = 'c';

x >= 8 || y === 'a'

The left side of the double pipe returns True where the right side is False. Thus, the result is True.

nurealam siddiq
  • 1,567
  • 10
  • 9
-2

The operator || means OR.

If either sig or graph are true or not null variables, callback function will receive a true argument.

n.maia
  • 77
  • 4
  • This is not how JavaScript works, though it's true for some other languages (like Java). – Pointy May 07 '15 at 18:03
  • My mistake. If the arguments are not boolean, the callback function will accept the first not null (or undefined) argument. – n.maia May 07 '15 at 18:15