Advertisement
aporokizzu

Short Circuit Conditionals

Mar 2nd, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // instead of
  2. const data = getData();
  3. if (data.error) {
  4.     console.log('There was an error.');
  5. }
  6.  
  7. // Do this
  8. (data.error) && console.log('There was an error.');
  9.  
  10. // or
  11. isThisTrue && doSomething() || doAnotherThing();
  12. // So if isThisTrue is true then it will call the doSomething() function, but if it’s false in the first place, doAnotherThing() will be called.
  13.  
  14. // Instead of writing this:
  15. if(truthy){
  16.     return doSomething();
  17. }else{
  18.     return "I will not do anything";
  19. }
  20.  
  21. // Write this:
  22. return (truthy && doSomething() || "I will not do anything");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement