Previous Example-|-Next Example-|-Return to Chapter Listing

Example 6.12:
The "?" Operator--a Shorthand 'if' Statement


The button calls function1(), which uses the shorthand conditional statement to determine how many seconds have passed since the start of the current minute, and so which message should be displayed:
This script achieves the same thing as the script in Example 6.7, but it's quicker to type.

This is the script we used: <SCRIPT LANGUAGE="JavaScript"> <!-- function function1() { var dToday = new Date() var nSeconds = dToday.getSeconds() var sText = (nSeconds > 30) ? "MORE" : "LESS" alert("We are " + sText + " than halfway through this minute. Seconds=" + nSeconds ) } //--> </SCRIPT> </HEAD> <BODY> <BR> <form> <input type="button" value=" Button A " onclick="function1()"> </form>
Previous Example-|-Next Example-|-Return to Chapter Listing