Mathematics has a rich set of relational operators.
These operators include equality and inequality tests (a =
b, a ≠ b), strict ordering tests
(a < b, a > b), and ordering
or equality tests (a
b, a
b).
All of the languages also define all these operators.
However, JavaScript and Visual Basic for Applications are written
using ASCII characters, and the ASCII character set does not include
the symbols ≠,
, or
.
Further, JavaScript actually defines assignment as an operator, rather
than a statement: so JavaScript already has an operator =, so it must
use something else in order to test for equality.
The character sequences used in each of the calculator, JavaScript, and Visual Basic for Applications for these relational operators are as follows:
Test |
Mathematics |
Calculator |
JavaScript |
VBA |
|---|---|---|---|---|
Equality |
= |
= |
== |
= |
Inequality |
≠ |
≠ |
!= |
<> |
Less than | < |
< |
< |
< |
Less than or equal to |
|
|
<= |
<= or =< |
Greater than |
> |
> |
> |
> |
Greater than or equal to |
|
|
>= |
>= or => |
There are several items to note in this table. These are discussed in the sections that follow.
Unsurprisingly, the calculator has distinct symbols for ≠,
, and
.
These are obtained by choosing the appropriate operation from the
relational operation menu.
This is not surprising as the calculator uses its own character set,
one optimized for the calculator’s functions.
JavaScript uses two equal signs, ==, as its equality operator, rather than one equal sign. As noted before, this is because JavaScript’s assignment is actually an operator, rather than a statement. This can cause some confusion, as the construct:
if (a = b)
{
statements;
}
is perfectly legal, although frequently not what is desired. Its effect is to assign b to a, then to test the value for equality with zero. If b (and now a) is non-zero, the statement sequence statements is executed. This can frequently be guarded against by forming the habit of putting all constants in an equality relational on the left side.
JavaScript also uses != as its inequality operator. This combines the unary logical negation operator, !, with the = assignment operator. This can be confusing, as JavaScript has a rich set of assignment operators, including +=, -=, *=, /=, <<=, >>=, >>>=, %=, &=, |=, and ^=. These assignment operators combine the operation (+, -, etc.) on the two operands with assignment to the left operand. !=, however, is unique among these operators formed by combining an operator with the assignment operator, in that it does not also produce an assignment.
JavaScript also has another equality-like comparison operator, ===, the identity operator. Given the pattern above, one would expect this operator to perform an equality test and to assign the results to the left operand, but it does not do that.
In JavaScript’s <= and >= operators, the order of the characters in the relational operator is important. The angle bracket must come before the equal sign. The constructs =< and => are syntax errors.
VBA uses a single equal sign, =, for its equality relational operator. It can do this because it can always tell the difference between an assignment and a relational operator. For example, the statement:
a = b = c
Compares the two quantities b and a, and assigns the result of that comparison to a.
VBA combines the less than sign, <, and the greater than sign, >, to form its inequality operator, <>.
Finally, VBA allows both of <= and =< for less than or equal to, and both of >= and => for greater than or equal to. The VBA programming environment, however, will automatically change the forms with the leading equal sign (=< and =>) to the forms with the trailing equal sign (<= and >=).
[ Previous page | Top of page | Next page ]
Copyright © 2002 Brian Hetrick
Page last updated 13 January 2002.
Tutorial
Building Blocks I
Expressions
Relational
Control Flow II
Basic I/O
Algorithms
A First Program
Answers
Modularization
Data Structures I
Recursion
Program Attributes
Building Blocks II
Algorithm Analysis
Structuring
Data Structures II
Abstract Types
Objects
Problem Analysis
Reference Card
![]()