Less Than or Equal to Operator in JavaScript

Learn how the less than or equal to operator works in JavaScript with number, string, and type coercion examples.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

Less Than or Equal to Operator in JavaScript

The less than or equal to operator in JavaScript is written as <=. It returns true when the left value is smaller than or equal to the right value, which makes it useful for range checks and ordering logic.

You will often use it when validating numeric thresholds, comparing dates, or checking sorted values. If your data comes in string form first, JavaScript compare dates and numeric cleanup steps often matter too.

Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.


Method 1: Compare numbers with <=

<= checks whether the left number is smaller than or equal to the right number.

javascript
console.log("number-compare-1:", 5 <= 3);
console.log("number-compare-2:", 3 <= 5);
Output

You should see 2 lines, in order: number-compare-1: false, number-compare-2: true.

Use this when you want a straightforward numeric comparison.


Method 2: Compare strings with <=

JavaScript compares strings by their Unicode values, not by length.

javascript
console.log("string-compare-1:", "Big" <= "Cat");
console.log("string-compare-2:", "Cat" <= "Big");
Output

You should see 2 lines, in order: string-compare-1: true, string-compare-2: false.

This is useful when you are sorting or comparing text values alphabetically.


Method 3: Compare strings and numbers with coercion

When a string contains a number, JavaScript coerces it to a numeric value before comparing.

javascript
console.log("mixed-compare-1:", "3" <= 5);
console.log("mixed-compare-2:", 5 <= "3");
console.log("mixed-compare-3:", "Java" <= 4);
Output

You should see 3 lines, in order: mixed-compare-1: true, mixed-compare-2: false, mixed-compare-3: false.

Use this carefully because type coercion can change the result in ways that are not obvious at first glance.


Summary

The less than or equal to operator in JavaScript is a direct way to compare numbers, strings, and mixed values. Use it for range checks and ordered comparisons, but keep type coercion in mind when the inputs are not already numeric.


Official documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …