Return to Kroll Design home page

JavaScript Tutorials

 
« Return

questionmark operator ?

The "?" operator is also called the conditional operator. With it you can create the equivalent of an if-else statement, with the following structure:
result = (condition) ? (do this if condition is true) : (do this if condition is false);

Code:

<script type="text/javascript">
//<![CDATA[a = 3;
b = 5;

c = (a > b) ? (a - b) : (b - a);

document.write("absolute value of a minus b = ", c);

//]]>
</script>

Output:



 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM