0
function querySearch (query) {                 
    var results = query ? $scope.allContacts.filter(createFilterFor(query)) : [];                 
    return results;
}

What the question mark ? does mean in here? Is it optional?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Mariem05
  • 37
  • 7
  • http://stackoverflow.com/questions/6259982/how-do-you-use-the-conditional-operator-in-javascript – yBrodsky Mar 06 '17 at 14:32
  • If `query` is `truthy` set `result` to the result of a filter using the query, otherwise set it to an empty array. – jEdgren Mar 06 '17 at 14:32
  • See [MDN JavaScript Reference - Conditional (ternary) Operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator). – georgeawg Mar 06 '17 at 19:48

1 Answers1

0

It means: If evaluation of query is truthy (typeof query != 'undefined' && query != null && query != 0 && query != false), then return the result of evaluating $scope.allContacts.filter(createFilterFor(query)) else return an empty array ([]).

In this case, it guarantees, that always an array is returned.

If the query ? is not performed and query is null, for instance, $scope.allContacts.filter(createFilterFor(query)) could throw an exeption, so this is sometimes used for safety.

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80