4. JavaScript Comparison Operators:
The JavaScript Comparison Operators allow programmers to test for more subtle conditions than the simple existence of an object (relative equality, inequality, etc.)
== | equal to
|
!= | not equal to
|
< | less than
|
> | greater than
|
>= | greater than or equal to
|
<= | less than or equal to
|
The operators are included in the statement of conditions using the following general syntax
if(value1 operator value2) { [statements];}
The values compared in the test (value1, value2) can be either variables, literal values and/or the result of one or more operations.
if(x operator 1) { [statements];}
if(x operator "one") { [statements];}
if(x operator y) { [statements];}
if(x operator (y-1)) { [statements];}
The examples below demonstrate the behaviors and basic syntax for each of the operators. Note that the examples assume the existence of variable (x) that is assigned a value of 1 (var x=1):
4.1 Equality:
The Equality Operator (x==y) returns true if x is equal to y, else false.
In this example the value of x is equal to one (1) so the comparison returns true:
Listing 4.1 Equality
var x = 1;
if(x==1) {document.write("Condition True");
} else {
document.write("Condition False");
}
Condition True
Note that the equality operator is two equal signs (==), and that using a single equal sign will result assigning rather than testing the values compared.
4.2 Inequality:
The Inequality Operator (x != y) returns true if x is not equal to y, else false.
In this example the value of x is equal to one (1) so the comparison returns false:
Listing 4.2 Inequality
var x = 1;
if(x!=1) {document.write("Condition True")
} else {
document.write("Condition False");
}
Condition False
4.3 Greater Than:
The Greater Than Operator (x > y) returns true if x is greater than y, else false.
In this example the value of x is not greater than one (1) so the comparison returns false:
Listing 4.3 Greater Than
var x = 1;
if(x>1) {document.write("Condition True");
} else {
document.write("Condition False");
}
Condition False
4.4 Greater Than/Equal To:
The Greater Than/Equal To Operator (x >= y) returns true if x is greater than or equal to y, else false.
In this example the value of x is not less than but is equal to one (1) so the comparison returns true:
Listing 4.4 Greater Than/Equal To
var x = 1;
if(x>=1) {document.write("Condition True");
} else {
document.write("Condition False");
}
Condition True
4.5 Less Than:
The Less Than Operator (x < y) returns true if x is less than y, else false.
In this example the value of x is not less than one (1) so the comparison returns false:
Listing 4.5 Less Than
var x = 1;
if(x<1) {document.write("Condition True");
} else {
document.write("Condition False");
}
Condition False
4.6 Less Than/Equal To:
The Less Than/Equal To Operator (x <= y) returns true if x is less than or equal to y.
In this example the value of x is not less than but is equal to one (1) so the comparison returns true:
Listing 4.6 Less Than/Equal To
var x = 1;
if(x<=1) {document.write("Condition True");
} else {
document.write("Condition False");
}
Condition True