O'Reilly logo

JavaScript™ 1.5 by Example by Kathie Kingsley-Hughes, Adrian Kingsley-Hughes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The if Statement

The if statement is an example of a flow control statement that has many uses.

The if statement enables your JavaScript script to choose between one of two alternative groups of statements to run (or execute). The choosing is done with a Boolean (true of false) condition.

Let's revisit the confirm box in this example and look at how you can make this type of message box work by using an if statement.

The first thing you must do is create a simple confirm box using the confirm() method:

<script language="JavaScript">
<!-- Cloaking device on!
confirm("Do you want to proceed with this book? Click OK to proceed otherwise click Cancel.");
// Cloaking device off -->
</script>

This now presents the user with two options: OK and Cancel ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required