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;
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;
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.