Comparison operators compare the values of two variables or statements and return the result of the comparison as either true or false. The result of the comparison can be used to control the flow of the program.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site
   WARNING!
What you learn from these Totally FREE
Einstein Newsletters could cause your friends to mistake you for someone else!
  [] automobiles
  [] business
  [] parenting
  [] computers
  [] contests
  [] education
  [] entertainment
  [] food/wine
  [] free stuff
  [] genealogy
  [] health/fitness
  [] home/garden
  [] humor
  [] marketing
  [] investing
  [] pets
  [] inspiration
  [] self-improve
  [] recreation
  [] travel
  [] womens stuff
  [] writing/reading
Click here and choose as many as you like!

Comparison Operators

Comparison operators compare the values of two variables or statements and return the result of the comparison as either true or false.

operatorcomparison
>greater than
<less than
==equal to
!=not equal to
<=less than or equal to
>=greater than or equal to

The result of the comparison can be used to control the flow of the program. The if/else structure is the most commonly used control structure.

var nPrice = 29.00;
var nHighest = 32.00;

if(nPrice < nHighest)
{
  document.write("buy it!");
}
else
{
  document.write("don\'t buy it.");
}

The code shown above would print "buy it." because the value of nPrice is lower than the value of nHighest. The comparison operator (<) returns true, and the code inside the if block is executed.

If the value of nPrice was higher than the value of nHighest, the result of the comparison would be false and the program flow would drop down to the else statement. The program would print "don't buy it."

The if structure is frequently used without the else block as shown below. In this case, if the value of nPrice was higher than the value of nHighest, the result of the comparison would be false and the program flow would drop through, not printing anything.

var nPrice = 29.00;
var nHighest = 32.00;

if(nPrice < nHighest)
{
  document.write("buy it!");
}

Another control structure is the do/while loop as shown below.

var nPrice = 64.00;
var nHighest = 32.00;

do
{
  --nPrice;
} while (nPrice > nHighest);

This structure loops through the code inside the curly brackets, which decrements the value of nPrice on each loop. Then it performs the comparison in side the brackets following the while statement to see if the value of nPrice is greater than the value of nHighest. If the result of the comparison is true, the program executes the statement inside the brackets, decrementing the value of nPrice again. This continues until the result of the comparison is false. Then the program flow breaks out of the loop.

Comparison operators compare the values of two variables or statements and return the result of the comparison as either true or false. The result of the comparison can be used to control the flow of the program.

Web Design Sections
Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269