Home / Advanced JavaScript Tutorials / Variable and expression shortcuts |
CodingForums Jump to...
- CSS Drive
- PHP Resources - Hotels & Vacations - Bad credit cards - JavaScript Menus |
Finally, let me show you a shortcut technique for compressing those often-used if-else statements. A default if-else statement spans four lines, like below: <script> The "?" conditional statement allows you to compact that into just one line, and has the following syntax: (condition) ? doiftrue : doiffalse Let's shove our four line code into the compression chamber, shall we? (navigator.appName=="Netscape") ? alert("Netscape user!") : alert("IE user!") The "?" conditional expression statement is being used more and more these days as scripts get longer and longer, and compactness actually becomes an issue. Some people argue against using the "?" statement, saying it's not as "intuitive" as the if-else statement. I say, get intuitive! -Tutorial introduction |
. http://javascriptkit.com |