Comparison Operators in JavaScript

Video Tutorial
FREE
Comparison operator thumbnail
This video belongs to
JavaScript Course With Certification: Unlocking the Power of JavaScript
9 modules
Certificate
Topics Covered

In the dynamic world of web development, JavaScript stands as a cornerstone language, driving interactivity and functionality across countless websites. Comparison operator in javascript are among its fundamental building blocks, essential tools that allow developers to make decisions and control the flow of their code. This article delves into the nuances of these operators, exploring how they compare values, manage type coercion, and ultimately shape the logic of JavaScript applications.

JavaScript Comparison Operators list

Comparison operator in javascript are used to compare two operands (simply values). A boolean value (either true or false) is returned based on comparing two values, and different actions or decisions are taken based on that returned boolean value. There are eight Comparison Operators in Javascript. Refer to the table given below.

JavaScript Comparison OperatorLiteral MeaningUse case
>greater thanIt returns true if the operand on the left of it is greater than the operand on the right of it
<less thanIt returns true if the operand on the left of it is less than the operand on the right of it
>=greater than or equal toIt returns true if the operand on the left of it is greater than or equal to the operand on the right of it
<=less than or equal toIt returns true if the operand on the left of it is less than or equal to the operand on the right of it
==equal toIt returns true if the operand on the left of it is (loosely)equal to the operand on the right of it
===strict equal toIt returns true if the operand on the left of it is (strictly)equal to the operand on the right of it
!=not equal toIt returns true if the operand on the left of it is (loosely) not equal to the operand on the right of it
!==strict not equal toIt returns true if the operand on the left of it is (strictly) not equal to the operand on the right of it

Equality (==)

The equality operator (==) compares two values for equality, after converting both values to a common type. Example:

Output: true because '5' (string) is converted to 5 (number) before comparison.

Inequality(!=)

The inequality operator (!=) checks if two values are not equal, with type coercion. Example:

Output: false because '5' (string) is converted to 5 (number), and they are considered equal.

Strict Equality (===)

Strict equality (===) compares both value and type, without converting them. Example:

Output: false because the types (string vs. number) are different.

Strict Inequality (!==)

Strict inequality (!==) checks if two values are not equal or not of the same type, without type coercion. Example:

Output: true because the types are different.

Greater Than (>)

The greater than operator (>) checks if the value on the left is greater than the value on the right. Example:

Output: false, because 5 is not greater than 10

Greater Than or Equal (>=)

The greater than or equal operator (>=) checks if the left value is greater than or equal to the right value. Example:

Output: true, because 5 is equal to 5

Less Than (<)

The less than operator (<) checks if the value on the left is less than the value on the right. Example:

Output: true because 5 is less than 10

Less Than or Equal (<=)

The less than or equal operator (<=) checks if the left value is less than or equal to the right value. Example:

Output: true because 10 is equal to 10

Supported Browsers

  1. Google Chrome: Excellent support for the latest JavaScript features, frequent updates.
  2. Mozilla Firefox: Strong JavaScript support, regular updates, developer-friendly.
  3. Safari: Good support, with some unique behaviors; important for iOS devices.
  4. Microsoft Edge: Based on Chromium, similar JavaScript support to Chrome.
  5. Internet Explorer: Limited support, requires polyfills for modern features; mostly relevant for legacy systems.

Conclusion

  • We have seen the Comparison Operators in JavaScript.
  • We got to know about different Comparison Operators in Javascript like >, <, >=, <=, ==, !=, etc.
  • We learned how comparison operator in javascript act on various data types.
  • We saw the behaviour of comparison operator in javascript when both the operands are of different data types.
  • We learned that comparison operator in javascript always return a boolean value, i.e. either true or false.
  • As Comparison operators return a boolean value, they are widely used with conditional javascript statements like if-else statements and loops like for loop, while loop, etc.
  • We have seen what strict equal operator (===) and not strict equal operator(!==) are.
  • We also saw the difference between equal operator(==) and Strict equal operator(===) in JavaScript.