theReverseFlick
theReverseFlick

Reputation: 6054

Execute 3 functions that return true/false and if all return true do something or else, no short circuiting

I have 3 JS functions a() b() c()

I want to execute all 3 and also check if all 3 return true then I want to call function yeah() or else call function boo()

I can use && but it will short circuit and may not execute all 3 functions if first or second returns false

So

      if(a() && b() && c()) { yeah(); } else { boo(); }

wouldn't work!

Can you suggest a better single line code?

Upvotes: 1

Views: 1704

Answers (2)

mellamokb
mellamokb

Reputation: 56779

If you want a one-liner, you can also use & instead of &&:

if(a() & b() & c()) { yeah(); } else { boo(); }

Or you can do this if you want to know exactly how many functions returned true:

if(a() + b() + c() == 3) { yeah(); } else { boo(); }

Sample: http://jsfiddle.net/DQkpM/1/

Upvotes: 5

HJW
HJW

Reputation: 23443

A & b & c will do the trick. It will execute all three functions.

Upvotes: 1

Related Questions