How to Use Relational Operators in JavaScript

Relational operators in JavaScript are used to compare two values, returning a Boolean value indicating the relation. They are fundamental in control flow and decision-making in code. This guide covers how to use them.

Understanding relational operators

Relational operators compare two operands and return true or false depending on the validity of the comparison. The most common relational operators are:

  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)

These operators are often used in loops and if statements to control the flow of the program based on numerical or sometimes string comparisons.

Comparing numbers

To compare two numbers, simply place a relational operator between them.

let a = 5; let b = 10; console.log(a > b); // Output: false console.log(a < b); // Output: true console.log(a >= 5); // Output: true console.log(b <= 10); // Output: true

Comparing strings

Relational operators can also be used to compare strings based on standard lexicographical ordering, using Unicode values.

let first = 'apple'; let second = 'banana'; console.log(first < second); // Output: true console.log(first > second); // Output: false

Edge cases in comparisons

Special cases arise when comparing different data types, or when dealing with NaN (Not-a-Number) and undefined.

console.log('2' > 1); // Output: true - string '2' is converted to number console.log('01' == 1); // Output: true - string '01' is converted to number console.log(true == 1); // Output: true - boolean true is converted to number console.log(false == 0); // Output: true - boolean false is converted to number console.log(NaN >= 0); // Output: false - NaN is never equal to any number console.log(undefined >= 0); // Output: false - undefined can't be compared to numbers

Best practices in relational comparisons

To avoid unexpected results in comparisons, especially when dealing with different data types, it is often best to use strict comparison operators (=== and !==) along with relational operators.

console.log(1 === parseInt('1')); // Output: true console.log('10' !== 10); // Output: true

Using strict comparison ensures that the operands are of the same type, which can prevent logical errors in your code.

Relational operators in control structures

Relational operators are crucial in if statements, loops, and ternary operators for making decisions.

let score = 75; // Using in if statement if (score >= 70) { console.log('Passing grade'); } // Using in a loop while (score > 0) { console.log(score); score--; } // Using in a ternary operator let result = score > 50 ? 'Pass' : 'Fail'; console.log(result);

Conclusion

Remember to consider the data types you're comparing and use strict comparison when needed. Relational operators are powerful tools for controlling the flow of your program, allowing you to write dynamic and responsive code.

Invite only

We're building the next generation of data visualization.