Language: EN

javascript-operadores-comparacion

Comparison Operators in JavaScript

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

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

The arithmetic operators in JavaScript 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 thanChecks if one value is greater than another
<Less thanChecks if one value is less than another
>=Greater than or equal toChecks if one value is greater than or equal
<=Less than or equal toChecks if one value is less than or equal

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 take data type into account.

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

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

Inequality (!=)

The inequality operator != compares two values and returns true if they are different, false if they are equal. This operator does not take data type into account.

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

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

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

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

5 > 3; // true
5 >= 5; // true

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

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

5 < 10; // true
5 <= 5; // true

It is recommended to use === instead of == (!== instead of !=) and to avoid unexpected behavior due to type coercion.