Skip to content

Try our new Crash Courses!

Buy one of our new Crash Courses, now hosted on Podia.

Comparison operators – JavaScript

Length: 10 minutes

Summary

You can use the following comparison operators with your conditional statements:

  • > (greater than)
  • < (less than)
  • >= (greater than or equal)
  • <= (less than or equal)
  • == (equality)
  • != (inequality)
  • === (identity)
  • !== (nonidentity)

Details

The greater than, less than, greater than or equal, and less than or equal operators work just like they do in your math class. The other operators have a few more rules to consider when using them. Note: the pieces of data on either side of the operator are called operands (in our examples, we’re only using numbers, but you can use strings and other data types as well).

Greater than

According to MDN, the greater than operator “returns true if the left operand is greater than the right operand.”

// true
if (1 > 0) { }
// false
if (1 > 2) { }

Less than

According to MDN, the less than operator “returns true if the left operand is less than the right operand.”

// true
if (1 < 2) { }
// false
if (1 < 0) { }

Greater than or equal

According to MDN, the greater than or equal operator “returns true if the left operand is greater than or equal to the right operand.”

// true
if (1 >= 0) { }
// true
if (1 >= 1) { }
// false
if (1 >= 2) { }

Less than or equal

According to MDN, the less than or equal “returns true if the left operand is less than or equal to the right operand.”

// true
if (1 <= 2) { }
// true
if (1 <= 1) { }
// false
if (1 <= 0) { }

Equality

The equality operator checks if the two operands are equal. If they are, then it returns true. If they aren’t, it returns false. If you are comparing a string to a number, and the string contains a number, it will attempt to convert the string to a number and then check if the two operands are equal (see the 2nd example below).

// true
if (1 == 1) { }
// true
if (1 == '1') { }
// false
if (1 == 0) { }

Inequality

The inequality operator checks if the two operands are not equal. It returns true if they are not equal, and false if they are equal. If you are comparing a string to a number, and the string contains a number, it will attempt to convert the string to a number and then check if the two operands are not equal (see the 3rd example below).

// true
if (1 != 0) { }
// false
if (1 != 1) { }
// false
if (1 != '1') { }

Strict equality

The strict equality operator checks whether two operands are equal. The two operands must be the same type to be considered equal, unlike the regular equality operator, so comparing strings and numbers will return false.

// true
if (1 === 1) { }
// false
if (1 === '1') { }
// false
if (1 === 0) { }

Strict inequality

The strict inequality operator checks whether two operands are not equal. If the two operands are different types, the operator will always return true, unlike the regular inequality operator.

// true
if (1 !== 0) { }
// true
if (1 !== '1') { }
// false
if (1 !== 1) { }

Exercises

Try the following statements in the console:

1 > 0
1 > 1
1 > 2

Then try them again but swap the > operator with the <, >=, <=, ==, ===, !=, and !== operators.

Reference

Relational operators on MDN

Back to: JavaScript Reference > JS Conditional Statements