Return to Kroll Web 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:




©2010 Kroll Web Design    davidarthurkroll@verizon.net    781.910.3694