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 ...

Get JavaScript™ 1.5 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.