3

I'm trying to understand the below block of syntax (taken from angular docs to represent the use of angular noop as an 'empty' function)

function foo(callback) {
  var result = calculateResult();
  (callback || angular.noop)(result);
}

I don't understand the '||' syntax. I tried looking it up but had trouble searching and wasn't really sure what to search.

Thinking of it as 'OR' syntax, it would mean to me if a function was assigned to the callback or if the function was assigned to angular noop would equal true and then a function is being called on true. But obviously that's wrong

Apologies in advance for the newbishness of the question.

- EDIT -

To clarify further, I'm aware of what it does guessing from the example. However, I'm trying to figure out what javascripts rule say that would cause the return statement for (callback1 || callback2) to return a function object instead of a boolean (as implied by the fact that in the example you can call the return of the sub-expression).

ThinkBonobo
  • 15,487
  • 9
  • 65
  • 80

2 Answers2

5

This bit

(callback || angular.noop)(result);

is short for:

if (callback) { 
    callback(result); 
} else {
    angular.noop(result); 
}

It takes advantage of the fact that || is lazily executed. The search term you're looking for is lazy evaluation. To explain the "why" on how this works, we can take a look at the ECMAScript specification, specifically 11.11 Binary Logical Operators,

The value produced by a && or || operator is not necessarily of type Boolean. The value produced will always be the value of one of the two operand expressions. Note that this doesn't mean that you can't depend on an expression such as:

if (a || b) // <-- logical expression will evaluate to the value of a or b, NOT true or false

because JavaScript evaluates boolean values as truthy or falsy, formally defined as the operation ToBoolean() in the ECMAScript specification.

From 9.2 ToBoolean

The abstract operation ToBoolean converts its argument to a value of type Boolean according to Table 11:

 Argument         
   Type           Result

 Undefined        false
 Null             false
 Boolean          The result equals the input argument (no conversion).
 Number           The result is false if the argument is +0, −0, or NaN; 
                      otherwise the result is true.
 String           The result is false if the argument is the empty String 
                      (its length is zero); otherwise the result is true.
 Object           True
jdphenix
  • 15,022
  • 3
  • 41
  • 74
  • with your help I think I figured out my issue. Could you please mention that the return for an || operation is not a bool but the actual value just for reference? I'll accept your answer in advance. – ThinkBonobo Apr 08 '15 at 05:17
  • @ThinkBonobo Top answer here http://stackoverflow.com/questions/3088098/in-javascript-what-does-it-mean-when-there-is-a-logical-operator-in-a-variable explains it more succinctly than I would – jdphenix Apr 08 '15 at 05:29
5

Basically the statement

(callback || angular.noop)(result);

Can be read as:

Call the function callback or angular.noop with the argument of result

If callback is not defined the OR (||) will be evaluated to call the angular.noop function passing the variable result to the function.

The angular.noop() function is a function that does nothing. More read here https://docs.angularjs.org/api/ng/function/angular.noop

Nico
  • 12,493
  • 5
  • 42
  • 62
  • Thanks. the || is actually returning a function not a boolean. Getting that piece makes the rest of it make sense to me now. – ThinkBonobo Apr 08 '15 at 05:18