The JavaScript Diaries: Part 3 - Page 3
[previous] [next]
The JavaScript Diaries: Part 3
Comparison Operators
Operator | Name | Description |
== | Equal | Returns true if both values are equal |
!= | Not Equal | Returns true if both values are not equal |
=== | Strict Equals | Returns true if both values are equal and are the same data type (Added in JavaScript 1.5) |
!== | Strict Not Equal | Returns true if both values are not equal or not the same data type (Added in JavaScript 1.5) |
> | Greater Than | Returns true if the value on the left side of the operator is greater than the value on the right side |
>= | Greater Than or Equal | Returns true if the value on the left side of the operator is greater than or equal to the value on the right side |
< | Less Than | Returns true if the value on the left side of the operator is less than the value on the right side |
<= | Less Than or Equal | Returns true if the value on the left side of the operator is less than or equal to the value on the right side |
These operators are used for making comparisons which are true or false. These
are pretty self-explanatory so I won't go into too much detail here. Notice
that the single equal operator ("=
") is not shown here. That is
because it is an assignment operator. It's used
for assigning a value to a variable or other type of container. i.e. var
myNum = 35
.
Comparison operators can be used for both numbers and strings. A value in a variable (number or string data type) can also be compared. The JavaScript interpreter will retrieve the value of the variable and then compare it with the other value.
Below are some comparisons. I've used several to give you a broader understanding of these comparison operators. Look at them very carefully and they should make sense.
|
When comparing two strings, JavaScript converts each character to its ASCII value, beginning on the left and continuing to the right. Using the ASCII equivalent, a lower case letter is greater in value than an upper case one. For instance, in ASCII code "A"=65 and "a"=97. So, "A" > "a" would be false while "a" > "A" would be true.
Assignment Operators
Operator | Name | Description |
= | Assign | Assigns the value on the right side of the operator to the variable on the left |
+= | Add and Assign | Adds the value on the right side of the operator to the variable on the left, and then assigns the new value to the variable on the left |
-= | Subtract and Assign | Subtracts the value on the right side of the operator from the variable on the left, and then assigns the new value to the variable on the left |
*= | Multiply and Assign | Multiplies the value on the right side of the operator by the variable on the left, and then assigns the new value to the variable on the left |
⁄= | Divide and Assign | Divides the variable on the left side of the operator by the value on the right side, and then assigns the new value to the variable on the left |
%= | Modulus and Assign | Divides the variable on the left side of the operator by the value on the right side, and then assigns the remainder to the variable on the left |
These operators are used for assigning values to variables, elements, and properties. Many of these operators are useful when utilized within loops, as some of them perform simple calculations on the variables. They are pretty much self-explanatory. They will come into play more when we begin studying loops. Examples of their use are shown below.
|
[previous] [next]
Created: May 13, 2005
URL: