The JavaScript Diaries: Part 3

By Lee Underwood

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.

  • 25 == 25 // ("25 is equal to 25") true
  • 25 == "25" // ("25 is equal to 25, after it has been converted from a string to a number" [The JavaScript interpreter will convert the string "25" to a number, 25.] ) true

  • 25 != 25 // ("25 is not equal to 25") false
  • 25 != 34 // ("25 is not equal to 34") true

  • 25 === 25 // ("25 is strictly equal to 25") true
  • 25 === "25" // ("the number 25 is strictly equal to the string '25'" [In this case, the data type of the values is taken into account and is not converted.] ) false

  • 25 !== "25" // ("the number 25 is strictly not equal to the string '25'") true

  • 25 > 24 // ("25 is greater than 24") true

  • 25 >= 25 // ("25 is greater than or equal to 25") true
  • 25 >= 15 // ("25 is greater than or equal to 15") true

  • 25 < 37 // ("25 is less than 37") true

  • 25 <= 25 // ("25 is less than or equal to 25") true
  • 25 <= 15 // ("25 is less than or equal to 15") false
  • 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.

    • Assign (=): x = y
      (We've already used this in assigning a value to a variable, i.e. var myNum = 15)
    • Add and Assign (+=): x += y means the same as x = x + y
    • Subtract and Assign (-=): x -= y means the same as x = x - y
    • Multiply and Assign (*=): x *= y means the same as x = x * y
    • Divide and Assign (/=): x /= y means the same as x = x / y
    • Modulus and Assign (%=): x %= y means the same as x = x % y



    Page 3 of 6

    Previous Page
    1 2 3 4 5 6
    Next Page

    Make a Comment

    Loading Comments...

    • Web Development Newsletter Signup

      Invalid email
      You have successfuly registered to our newsletter.
    •  
    •  
    •  
    Thanks for your registration, follow us on our social networks to keep up-to-date