Table of Contents
One of the different comparison operators is the less than equal to operator (
The less than or equal to operator (<=) compares the values of two operands and returns true if the first operand is less than or equal to the second operand. Otherwise, it returns false.
Comparison operators including less than or equal to operators can be used to compare numbers, strings, or even objects.
Let’s illustrate the behavior of the operator using different examples.
For string comparison, the alphabet characters from
Z occurs in ascending order, and so
Z is greater than
A. In the example below, the
"Big" is less than
"Cat" and vice versa.
console.log("Big" <= "Cat"); console.log("Cat" <= "Big");
The typical numerical order applies with the less than or equal to operator.
console.log(5 <= 3); console.log(3 <= 5);
String and Number Comparison
In cases where we are comparing strings and numbers using the less than or equal to operator, type coercion is triggered on the string, and so if the string contains numbers, number comparisons are executed.
console.log("3" <= 5); console.log(5 <= "3"); console.log("Java" <= 4);
true false false
In summary, the less than or equal operator can be used on numbers and strings to find which values are lesser or equal.