Menu

Javascript Comparisons

Often you want to compare a value of one variable with one of another variable, or the value of a variable against a literal value, for example the value in an if-statement.

An example of a comparison: if you want to compare the value of the day of the week to “Sunday”, you can do this by checking todaysDate == "Sunday".

Don’t mix up = with ==, the first is an assignment, the second one is a comparison.

Comparisons:

  • x == y – true if x and y are equal
  • x != y – true if x and y are not equal
  • x > y – true if x is greater than y
  • x >= y – true if x is greater or equal to y
  • x < y – true if x is less than y
  • x <= y – true if x is less or equal to y
  • x && y – true if x and y are true
  • x || y – true if x or y are true
  • !x – true if x is false