4

I was reading through Ionic's source and stumbled on this part here.

fromTemplateUrl: function(url, options, _) {
  var cb;
  //Deprecated: allow a callback as second parameter. Now we return a promise.
  if (angular.isFunction(options)) {
    cb = options;
    options = _;
  }
  return $ionicTemplateLoader.load(url).then(function(templateString) {
    var modal = createModal(templateString, options || {});
    cb && cb(modal);
    return modal;
  });
}

What's the syntax in line 10 , "cb && cb(modal);" , called and what does it do?

The conditional && is confusing me

AdrianLoer
  • 624
  • 3
  • 11

2 Answers2

3

thanks everyone, the answer by jon, a shorter way for

if(cb) cb(modal)

combined with the explanation at Assignment with double ampersand "&&" answer my question

I'll remember this as "short if exists syntax"

Community
  • 1
  • 1
AdrianLoer
  • 624
  • 3
  • 11
2
undefined && 42 // undefined, the second part wouldn't be evaluated
'defined' && 42 // 42
undefined || 42 // 42
'defined' || 42 // 'defined', the second part wouldn't be evaluated

That way this thing is working.

Unrealsolver
  • 143
  • 4