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());

Related posts:

  1. JavaScript Snippets: IF Statements Optimization
  2. Quick Look at JavaScript Objects
  3. JavaScript optimization. Optimizing IF statements.
This entry was posted in javascript, micro tutorial and tagged , , , , , , , . Bookmark the permalink.

3 Responses to JavaScript Snippets: IF Statements Optimization Part 2

  1. Masraf says:

    Cool

  2. balbir in. says:

    wow, this is the first time I see some optimization like that!

  3. ernesto says:

    @balbir in. – you gotta be very late

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">