Code Premix

Comparison Operators in JavaScript

📅May 19, 2024

Comparison operators in JavaScript are the tools that help you play the matchmaker for your code. They allow you to compare values and decide whether they’re equal, not equal, greater, or smaller, guiding your code’s decision-making process. Let’s take a stroll through the world of JavaScript comparison operators and learn how they facilitate the art of comparison in your scripts.

List of Operators

1. Equal (==)

Checks if two values are equal, often ignoring their types.

let isEqual = 5 == '5'; // Result: true

2. Strict Equal (===)

Compares values and types strictly, ensuring both are identical.

let isStrictEqual = 5 === '5'; // Result: false

3. Not Equal (!=)

Checks if two values are not equal.

let notEqual = 10 != 5; // Result: true

4. Strict Not Equal (!==)

Similar to strict equal, but it evaluates inequality and types.

let strictNotEqual = 10 !== '10'; // Result: true

5. Greater Than (>) and Less Than (<)

Compares numerical values to determine greater or smaller.

let greaterThan = 15 > 10; // Result: true
let lessThan = 5 < 8; // Result: true

6. Greater Than or Equal (>=) and Less Than or Equal (<=)

Includes equality, checking if a value is greater than or equal to, or less than or equal to, another value.

let greaterThanOrEqual = 20 >= 20; // Result: true
let lessThanOrEqual = 3 <= 2; // Result: false

Conclusion

Comparison operators in JavaScript are the matchmakers in your code, helping you determine relationships between values. Whether you’re checking equality, inequality, or comparing numeric values, these operators are your trusted companions in decision-making within your code.

Happy coding!

With comparison operators as your ally, your code becomes a savvy matchmaker, orchestrating connections seamlessly.