Monday, July 20, 2009

JavaScript: ? = If Else

This is not directly related to APEX however it will help if you use a lot of JavaScript

When I first started to develop web based applications and used JavaScript I came across some JS code with a ? in it. I didn't know what it was for and Googling "JavaScript ?" didn't help either. Here's a quick summary on how it works

Boolean ? true action : false action

So this:

var x = 2;
if (x > 1)
window.alert('True');
else
window.alert('False');


Becomes this:

var x = 2;
x>1 ? window.alert('True') : window.alert('False') ;

No comments:

Post a Comment

Post a Comment