This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.
- What are Comparison Operators in JS?
- What is “!=” in JS?
- Other Comparison Operators
- Closing thoughts
Comparison operators in programming languages are used to compare two values. These operators return a boolean value (true or false) based on the condition. Hence these operators are used in decision making or as conditional statements for loops.
However, the “!==” or Strict inequality operator does not attempt to do so and returns false if the values are unequal or of different types.
Both these operators solve different purposes and hence I would recommend practicing them to facilitate further understanding.
console.log(5 != 10); // expected output: true console.log(10 != 10); // expected output: false console.log(10 != '10'); // expected output: false console.log(10 !== '10'); // expected output: true
In the first case, it returned true as the values were different. In the second and third cases, it returned a false cause the values are the same. Do note that in the latter case even though we passed 10 as a string the operator was able to compare both the values.
In the last case, we used the strict inequality operator and it returned true as the values were of different types.
- Equal to (==) - Check if two values are equal
- Strict equal to (===) - Checks is two values are equal and of similar type
- Greater than (>) - Checks if the value on the left is greater than the value on the right
- Greater than or equal to (>=) - Checks if the value is greater than or equal to the value on the right
- Less than (<) - Checks if the value on the left is less than the value on the right
- Less than or equal to (<=) - Checks if the value is less than or equal to the value on the right
console.log(5 == 10); // expected output: false console.log(10 === 10); // expected output: true console.log(10 ==='10'); // expected output: false console.log(5 > 10); // expected output: false console.log(5 >= 5); // expected output: false console.log(5 < 10); // expected output: true console.log(5 <= 5); // expected output: true
Once you are done with comparison operators do have a look at logical operators.