• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 6. Controlling Your Code I: Test... > Using if() to Make True/False Decisi...

Using if() to Make True/False Decisions

A smart script performs tests on its environment and then decides what to do next based on the results of each test. For example, suppose you've written a function that uses one of its arguments as a divisor in an expression. You should test the argument before using it in the expression to make sure that it isn't 0. If it is, you could then display a message that alerts the user of the illegal argument.

The most basic test is the simple true/false decision (which could also be seen as a yes/no or an on/off decision). In this case, your program looks at a certain condition, determines whether it is currently true or false, and acts accordingly. As you might expect from the discussion of expressions in Chapter 5, "Building JavaScript Expressions," comparison and logical expressions (which, you'll recall, always return a true or false result) play a big part here.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint