javascript-operadores-comparacion

Comparison Operators in JavaScript

  • 3 min

Comparison operators allow us to compare values and obtain a boolean result (true or false) based on the relationship between the values.

These operators allow us to compare two values and determine if they are equal, different, greater, or less than, and are fundamental for evaluating expressions and making decisions.

JavaScript’s comparison operators are:

OperatorNameDescription
==EqualityCompares if two values are equal
===Strict equalityCompares if two values and their type are identical
!=InequalityCompares if two values are different
!==Strict inequalityCompares if two values and their type are identical
>Greater thanVerifies if one value is greater than another
<Less thanVerifies if one value is less than another
>=Greater than or equal toVerifies if one value is greater than or equal to
<=Less than or equal toVerifies if one value is less than or equal to

If you want to learn more, check out the Introduction to Programming Course

List of comparison operators

Equality (==)

The equality operator == compares two values and returns true if they are equal, false if they are different. This operator does not consider data types.

5 == 5; // true
5 == '5'; // true, the comparison does not consider the type
Copied!

Strict equality (===)

The strict equality operator === compares two values and returns true if they are equal and of the same type, false if they are different.

5 === 5; // true
5 === '5'; // false, the types are different
Copied!

Inequality (!=)

The inequality operator != compares two values and returns true if they are different, false if they are equal. This operator does not consider data types.

5 != 3; // true
5 != '5'; // false, the comparison does not consider the type
Copied!

Strict inequality (!==)

The strict inequality operator !== compares two values and returns true if they are different or of different types, false if they are equal and of the same type.

5 !== 3; // true
5 !== '5'; // true, the types are different
Copied!

Greater than (>), greater than or equal to (>=)

These operators compare two numeric values and return true if the first value is greater than (or greater than or equal to) the second, false if it is not.

5 > 3; // true
5 >= 5; // true
Copied!

Less than (<), less than or equal to (<=)

These operators compare two numeric values and return true if the first value is less than (or less than or equal to) the second, false if it is not.

5 < 10; // true
5 <= 5; // true
Copied!

It is recommended to use === instead of == (!== instead of !=) to avoid unexpected behaviors due to type conversions.