JavaScript less than or equal to operator (<=
) returns true
if the left operand is less than or equal to the right operand, and false
otherwise.
x <= y
JavaScript less than or equal to
Simple example code function first evaluates if the condition (num <=25) evaluates to true converting num to a number if necessary.
If it does, it returns the statement between the curly braces (“Smaller Than or Equal to 25”). If it doesn’t, it checks if the next condition is true (returning “Smaller Than or Equal to 25”). Otherwise the function will return “More Than 50”.
<!DOCTYPE html>
<html>
<body>
<div></div>
<script>
function lessEqual(num) {
if (num <= 25) return "Smaller Than or Equal to 25";
if (num <= 50) return "Smaller Than or Equal to 50";
return "More Than 50";
}
console.log(lessEqual(50));
console.log(lessEqual(10));
console.log(lessEqual(75));
</script>
</body>
</html>
Output:
More examples
console.log(5 <= 3);//false
console.log(3 <= 3); // true
// Compare bigint to number
console.log(3n <= 5); // true
console.log('aa' <= 'ab'); // true
String to string comparison
console.log("a" <= "b"); // true
console.log("a" <= "a"); // true
console.log("a" <= "3"); // false
String to number comparison
console.log("5" <= 3); // false
console.log("3" <= 3); // true
console.log("3" <= 5); // true
console.log("hello" <= 5); // false
console.log(5 <= "hello"); // false
Number to Number comparison
console.log(5 <= 3); // false
console.log(3 <= 3); // true
console.log(3 <= 5); // true
Comparing Boolean, null, undefined, NaN
console.log(true <= false); // false
console.log(true <= true); // true
console.log(false <= true); // true
console.log(true <= 0); // false
console.log(true <= 1); // true
console.log(null <= 0); // true
console.log(1 <= null); // false
console.log(undefined <= 3); // false
console.log(3 <= undefined); // false
console.log(3 <= NaN); // false
console.log(NaN <= 3); // false
Do comment if you have any doubts or suggestions on this JS comparison topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version