Imagine you are a farmer who wants to sell his crops at the market. You have several baskets of apples, each containing a different number of apples. You want to sort the baskets based on the number of apples in each one so that you can price them correctly. You decide to use comparison operators in JavaScript to help you with this task.
First, you use the less than operator (<) to compare the number of apples in each basket. You start with the smallest basket and compare it with the next basket. If the number of apples in the first basket is less than the number of apples in the second basket, you move the first basket to the front of the line. You repeat this process until all the baskets are sorted from smallest to largest.
Just like the farmer compares his apples, comparison operators in JavaScript are used to compare values and return a boolean (true or false) result. These operators are used in conditional statements, loops, and other logical operations. It is important to understand the different types of comparison operators in JavaScript to write effective code.
There are 8 comparison operators in JavaScript, listed below:
The equal to operator compares two values for equality. It returns true if the values are equal and false if they are not equal.
Loading...
The not equal to operator compares two values for inequality. It returns true if the values are not equal and false if they are equal.
Loading...
The strict equal to operator compares two values for equality and type. It returns true if the values are equal and of the same type, and false if they are not equal or of a different type.
Loading...
The strict not equal to operator compares two values for inequality and type. It returns true if the values are not equal or of a different type, and false if they are equal and of the same type.
Loading...
The greater than operator compares two values to check if the left-hand side value is greater than the right-hand side value. It returns true if the left-hand side value is greater than the right-hand side value, and false otherwise.
Loading...
The less than operator compares two values to check if the left-hand side value is less than the right-hand side value. It returns true if the left-hand side value is less than the right-hand side value, and false otherwise.
Loading...
The greater than or equal to operator compares two values to check if the left-hand side value is greater than or equal to the right-hand side value. It returns true if the left-hand side value is greater than or equal to the right-hand side value, and false otherwise.
Loading...
The less than or equal to operator compares two values to check if the left-hand side value is less than or equal to the right-hand side value. It returns true if the left-hand side value is less than or equal to the right-hand side value, and false otherwise.
Loading...
We can combine comparison operators with logical operators to create more complex conditions.
AND (&&) Operator
The AND operator returns true if both the left-hand side and right-hand side expressions are true. Otherwise, it returns false.
Loading...
OR (||) Operator
The OR operator returns true if either the left-hand side or the right-hand side expression is true. Otherwise, it returns false.
Loading...
NOT (!) Operator
The NOT operator returns the opposite of the expression's truth value. If the expression is true, it returns false. If the expression is false, it returns true.
Loading...
When comparing numbers, JavaScript uses the same comparison operators as it does for other data types. For example, we can use the greater than operator (>) to check if one number is greater than another.
Loading...
We can also use other comparison operators like less than (<), greater than or equal to (>=), and less than or equal to (<=) to compare numbers.
Loading...
When comparing strings, JavaScript compares them based on their Unicode values. The Unicode value of a character is a unique number that represents it in the Unicode character set. To compare strings, we can use the same comparison operators that we use for numbers.
Loading...
In the example above, "apple" is less than "banana" because its first character "a" has a lower Unicode value than "b". Similarly, "cat" is greater than "dog" because its first character "c" has a higher Unicode value than "d".
Sometimes, we may need to compare a number with a value of another data type like a string or boolean. In JavaScript, when we compare a number with a string or boolean, the value of the string or boolean is converted to a number before the comparison is made.
Loading...
In the first example above, the string "5" is converted to the number 5 before it is compared to the number 5. In the second example, the boolean value true is converted to the number 1 before it is compared to the number 2. This is known as type coercion.
To avoid unexpected results due to type coercion, it is recommended to use the strict equality operator (===) which compares both the value and the data type.
Loading...
In the example above, both comparisons return false because the data types of the operands are different.
There are many real-life applications of comparison operators in JavaScript. Here are a few examples:
Top Tutorials
Related Articles