function querySearch (query) {
var results = query ? $scope.allContacts.filter(createFilterFor(query)) : [];
return results;
}
What the question mark ?
does mean in here? Is it optional?
function querySearch (query) {
var results = query ? $scope.allContacts.filter(createFilterFor(query)) : [];
return results;
}
What the question mark ?
does mean in here? Is it optional?
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.