Learn How to Compare Values in Java Script, Lesson 14
Comparing Values
One of the things you will find yourself doing in your scripts is comparing the values of variables. For example, you might take one action if a variable has a value less than 10 and a different action when the variable is greater than 10. Table 2.4 provides a list of JavaScript and JScript comparison operators you can use in your scripts.
JAVASCRIPT AND JSCRIPT COMPARISON OPERATORS
The following JavaScript demonstrates the use of the greater-than operator. In this example, the two variables x and y are initialized with values of 12 and 5, respectively. Next, the script tests to see whether x is greater than 10. This condition is true, so the script writes a message to the browser window. Then the script tests the value of y to see whether it is greater than 10. This test is false, so no action is taken.
<HTML>
<HEAD>
<TITLE>Script 2.5 – Example of a Value Comparison</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/JavaScript”>
<!– Start hiding JavaScript statements
var x = 12;
var y = 5;
if (x > 10);
{
document.write(“x is greater than 10!”);
}
if (y > 10)
{
document.write(“y is greater than 10!”);
}
// End hiding JavaScript statements –>
</SCRIPT>
</BODY>
</HTML>