1

I was recently going through some JavaScript code (written by someone else) and it had something like this:

var exports = exports || null;

What does this mean? Is this the equivalent of:

var exports = exports ? exports : null;
fur866
  • 383
  • 1
  • 3
  • 12
  • 2
    Yes. They both are equivalent. – Tushar Dec 06 '16 at 11:38
  • Yes. Check this out: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_OR – DontVoteMeDown Dec 06 '16 at 11:39
  • 1
    It _still_ means *or* in that example. exports or null. – Quentin Dec 06 '16 at 11:39
  • See also http://stackoverflow.com/questions/4446433/how-does-javascript-logical-assignment-work – T.J. Crowder Dec 06 '16 at 11:43
  • @Tushar: One significant difference is that `exports` is only evaluated once in the `||` case, but twice in the conditional operator case. Doesn't matter for the above, of course, but `var x = a() || b();` is very different from `var x = a() ? a() : b();` – T.J. Crowder Dec 06 '16 at 12:12

1 Answers1

0

What does “||” refer to in JavaScript apart from Or?

Nothing, but its definition of "or" is not the same as some other languages.

The ||operator evaluates its left-hand operand and if that result is truthy, that's the result of the operation; otherwise, the right-hand operand is evaluated and that result is the result of the operation.

So it doesn't necessarily result in a boolean value. In your example, exports || null will be the value of exports if it's truthy, or null if it isn't.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639