Comparison Operators

Please Click on this link to access the resource folder for this lesson.

Comparison operators

  • Comparison operators are used to apply boolean logic to operands. E.g 
    • Greater than: a > b
    • Less than: a < b
    • Greater or equals: a >= b
    • Less than or equals: a <= b
    • Equals: a == b, Strict Equality a === b e.g 0 === false
    • Not equals: a != b
  • All comparison operators return a boolean value
  • To see whether a string is greater than another, JavaScript uses the so-called “dictionary” or “lexicographical” order. In other words, strings are compared letter-by-letter.
  • Please note the double equality sign == means the equality test, while a single one a = b means an assignment.
  • To see whether a string is greater than another, JavaScript uses the so-called “dictionary” or “lexicographical” order. In other words, strings are compared letter-by-letter.
  • When comparing values of different types, JavaScript converts the values to numbers. alert( ‘2’ > 1 )
  • For boolean values, true becomes 1 and false becomes 0.
  • A regular equality check == has a problem. It cannot differentiate 0 from false: 0 == false, ” == false. 
  • These are JavaScript false values. Use a strict equality check instead. (0 === false ); // false, because the types are different

Other operators

  • The assignment operator (“=”) is used to assign values to a variable
  • The increment (“++”) and decrement (“–“) operators
  • Bitwise operators  : AND ( & ), OR ( | ), XOR ( ^ ), NOT ( ~ ), LEFT SHIFT ( << ), RIGHT SHIFT ( >> ), etc 
  • Assignment = is also an operator. It is listed in the precedence table with the very low priority.
  • Bitwise operators are used very rarely, when we need to fiddle with numbers on the very lowest (bitwise) level. 
  • Let’s note that an assignment = is also an operator. It is listed in the precedence table with the very low priority of 2. That’s why, when we assign a variable, like x = 2 * 2 + 1, the calculations are done first and then the = is evaluated, storing the result in x.
  • Increasing or decreasing a number by one is among the most common numerical operations. So, there are special operators for it:
  • Bitwise operators are used very rarely, when we need to fiddle with numbers on the very lowest (bitwise) level. We won’t need these operators any time soon, as web development has little use of them, but in some special areas, such as cryptography, they are useful. You can read the Bitwise Operators chapter on MDN when a need arises.
Scroll to Top