Comparison Operators(JavaScript)

Japanese version.

These operators are used to compare data types like numbers and strings. Comparison operators generate a Boolean value of true or false.

Explanation

== Equal to

The comparison operator == in JavaScript compares the left and right operands to determine whether they are equal. However, there is an important thing to note about this operator. The == operator performs type conversion as needed before the comparison. This means that it may convert one or both operands to a different type to make their types the same before the comparison.

For example, consider the following code:

console.log(5 == '5'); // true

In this example, the == operator is used to compare the numeric value 5 on the left with the string value '5' on the right. The string is converted to a number before the comparison is made. Because '5' can be converted to the number 5, the expression is true, and the console will log true.

However, this behavior can sometimes be confusing. For example, consider the following code:

console.log(0 == ''); // true

In this example, the == operator is used to compare the numeric value 0 on the left with the empty string '' on the right. Because the empty string is converted to a number, and the number value of the empty string is 0, the expression is true, and the console will log true.

This behavior can lead to unexpected comparison results. Therefore, it is recommended to use the === operator instead of ==. The === operator performs a strict comparison that only returns true if both the value and the type are the same.

=== Strictly equal to

Here's an example of using the == and === operators to compare a number and a string:

console.log(5 == '5'); // true
console.log(5 === '5'); // false

=== Since the operator is safer, this should be used as the main one.

!= Not equal to

The comparison operator != in JavaScript compares the left and right operands to determine whether they are not equal. Similar to the == operator, the != operator performs type conversion as needed before the comparison. This means that it may convert one or both operands to a different type to make their types the same before the comparison.

For example, consider the following code:

console.log(5 != '5'); // false

In this example, the != operator is used to compare the numeric value 5 on the left with the string value '5' on the right. The string is converted to a number before the comparison is made. Because '5' can be converted to the number 5, the expression is false, and the console will log false.

The != operator returns true if the left and right operands are not equal, meaning it has the opposite meaning of the == operator. However, like the == operator, the != operator can produce unexpected comparison results because of its type conversion behavior. Therefore, it is recommended to use the !== operator instead of !=. The !== operator performs a strict comparison that only returns true if both the value and the type are not the same.

!== Strictly not equal to

Here's an example of using the != and !== operators to compare a number and a string:

console.log(5 != '5'); // false
console.log(5 !== '5'); // true

!== Since the operator is safer, this should be used as the main one.

< Less than

The < comparison operator in JavaScript returns true if the left operand is less than the right operand. It can compare values of various types, such as numbers and strings. Before making the comparison, both operands are converted to the same type.

For numeric comparisons, the < operator performs a straightforward comparison of the numeric values:

console.log(5 < 10); // true
console.log(10 < 5); // false

For string comparisons, the < operator compares the Unicode code points of the strings. It compares the first character of each string, and if they are the same, it moves on to the next character. In this way, it determines the alphabetical order of the strings:

console.log('apple' < 'banana'); // true
console.log('banana' < 'apple'); // false

> Greater than

The > comparison operator in JavaScript returns true if the left operand is greater than the right operand. Like the < operator, it can compare values of various types, and the values are converted to the same type before the comparison.

For numeric comparisons, the > operator performs a straightforward comparison of the numeric values:

console.log(10 > 5); // true
console.log(5 > 10); // false

For string comparisons, the > operator compares the Unicode code points of the strings. It compares the first character of each string, and if they are the same, it moves on to the next character. In this way, it determines the alphabetical order of the strings:

console.log('banana' > 'apple'); // true
console.log('apple' > 'banana'); // false

<= Less than or equal to

The <= comparison operator in JavaScript returns true if the left operand is less than or equal to the right operand. Like the < operator, it can compare values of various types, and the values are converted to the same type before the comparison.

For numeric comparisons, the <= operator performs a straightforward comparison of the numeric values:

console.log(10 <= 5); // false
console.log(5 <= 10); // true
console.log(5 <= 5); // true

For string comparisons, the <= operator compares the Unicode code points of the strings. It compares the first character of each string, and if they are the same, it moves on to the next character. In this way, it determines the alphabetical order of the strings:

console.log('apple' <= 'banana'); // true
console.log('banana' <= 'apple'); // false
console.log('apple' <= 'apple'); // true

>= Greater than or equal to

The >= comparison operator in JavaScript returns true if the left operand is greater than or equal to the right operand. Like the > operator, it can compare values of various types, and the values are converted to the same type before the comparison.

For numeric comparisons, the >= operator performs a straightforward comparison of the numeric values:

console.log(10 >= 5); // true
console.log(5 >= 10); // false
console.log(5 >= 5); // true

For string comparisons, the >= operator compares the Unicode code points of the strings. It compares the first character of each string, and if they are different, it determines the alphabetical order of the strings. If the first characters are the same, it moves on to the next character, and so on:

console.log('apple' >= 'banana'); // false
console.log('banana' >= 'apple'); // true
console.log('apple' >= 'apple'); // true

---

Links

JavaScript Articles