JavaScript Relational Operators

JavaScript Relational Operators are used to compare two values. These operators return a boolean value (true or false) based on the relationship between the operands.

List of Relational Operators:

Operator Description Example Result
== Equal to 5 == '5' true
!= Not equal to 5 != '5' false
=== Strict equal to (checks type and value) 5 === '5' false
!== Strict not equal to 5 !== '5' true
> Greater than 5 > 3 true
< Less than 5 < 3 false
>= Greater than or equal to 5 >= 5 true
<= Less than or equal to 5 <= 5 true

1. Equal to (==)

The Equal to operator compares two values for equality, performing type conversion if necessary.

Example:

console.log(5 == '5'); // true (type conversion occurs)
console.log(5 == 5);   // true
console.log(5 == 6);   // false

Try It Now

2. Not Equal to (!=)

The Not Equal to operator checks if two values are not equal, performing type conversion if necessary.

Example:

console.log(5 != '5'); // false (type conversion occurs)
console.log(5 != 6);   // true

Try It Now

3. Strict Equal to (===)

The Strict Equal to operator checks for equality without performing type conversion. Both the value and type must be the same.

Example:

console.log(5 === '5'); // false (different types)
console.log(5 === 5);   // true

Try It Now

4. Strict Not Equal to (!==)

The Strict Not Equal to operator checks if two values are not equal and ensures that their types are not the same.

Example:

console.log(5 !== '5'); // true (different types)
console.log(5 !== 5);   // false

Try It Now

5. Greater than (>)

The Greater than operator checks if the left operand is greater than the right operand.

Example:

console.log(5 > 3); // true
console.log(3 > 5); // false

Try It Now

6. Less than (<)

The Less than operator checks if the left operand is less than the right operand.

Example:

console.log(5 < 3); // false
console.log(3 < 5); // true

Try It Now

7. Greater than or Equal to (>=)

The Greater than or Equal to operator checks if the left operand is greater than or equal to the right operand.

Example:

console.log(5 >= 5); // true
console.log(5 >= 3); // true

Try It Now

8. Less than or Equal to (<=)

The Less than or Equal to operator checks if the left operand is less than or equal to the right operand.

Example:

console.log(5 <= 5); // true
console.log(3 <= 5); // true

Try It Now

Type Coercion in Relational Operators:

  • == and !=: These operators perform type coercion. For example, a string "5" is considered equal to a number 5.
  • === and !==: These operators do not perform type coercion. The comparison considers both the value and type.

Comparing Strings:

When comparing strings, JavaScript compares their Unicode values character by character.

Example:

console.log('apple' > 'banana'); // false
console.log('apple' < 'banana'); // true

Try It Now

Comparing Different Types:

JavaScript converts the operands to the same type when using non-strict comparison operators (== and !=).

Example:

console.log(5 == '5');  // true
console.log(5 === '5'); // false
console.log(5 > '3');   // true

Try It Now

Summary:

  • == and !=: Allow type conversion.
  • === and !==: Strict comparison, no type conversion.
  • >, <, >=, <=: Used for numerical and string comparisons.
  • Always use === and !== for stricter comparison to avoid unexpected results due to type coercion.