Relational, logical, and bitwise operators

Objectives

  • Use relational operators
  • Use logical operators
  • Evaluate bitwise operators

Comparison operators

  • == equal to
  • != not equal to
  • < less than
  • > greater than
  • <= less than or equal to
  • >= greater than or equal to
  • === equal to and same data type
  • !== not equal to and/or different data types

Logical operators

  • !a NOT: returns the opposite (true/false) of the operand
  • a && b AND: returns true iff both operands are true
  • a || b OR: returns true iff either operand is true

Bitwise operators

  • a & b bitwise AND; returns 1 for each bit that is 1 in both operands
  • a | b bitwise OR; returns 1 for each bit that is 1 in either operand
  • a ^ b bitwise XOR; returns 1 for each bit that is 1 in only one operand
  • ~a bitwise NOT; inverts bits of operand
  • a << b shifts bits of a left by b bits (shifts in zeros)
  • a >> b shifts bits of a right by b bits (propogates sign bit)
  • a >>> b shifts bits of a right by b bits (shifts in zeros)

Boolean values in JavaScript

  • false: false, 0, zero-length strings, null, undefined, NaN evaluate to false
  • true: everything that isn't false evaluates to true
  • !!(expr) helps "booleanize" an expression; many Boolean values in JavaScript won't equate even if they are both "truthy" or "falsey". A double not (!!) helps get most of the values into a more compatible state for comparisons. The parentheses are important for when a ! operator might take precedence over an operator in the expression and change its meaning.
  • Special note: NaN is not equal to NaN