Tag Archives: Error

JavaScript Snippets: IF Statements Optimization Part 2

What if …

continuing from the previous post let assume there’s the following code:

if ( thisIsTrue ) {
   if ( myFuncReturnsTrue() ) {
      printMe('success');
   } else {
      printMe('error');
   }
}

This can be easily ported to:

thisIsTrue && (myFuncReturnsTrue() ? printMe('success') : printMe('error'));

Source

You can see the demo source here. Here’s a snippet:

function func1() {
	alert('func1');	
}
function func2() {
	alert('func2');	
}
 
var a = true;
var b = false;
 
a && (b ? func1() : func2());