Less Than or Equal to in JavaScript

Less Than or Equal to in JavaScript | The “less than or equal to” operator evaluates two expressions’ values and compares them. The “less than or equal to” operator returns true if the expression on the left evaluates to a value that is less than or equal to the evaluated value of the expression on the right; else, the operator returns false. Less than or equal to is denoted by the sign <=.

The “less than or equal to” operator will also, at runtime, transform the values of the expressions to the same type before comparing the values of the results if either expression has a JavaScript type (such as variables declared with var, function, or external). Also see:- JavaScript Operator Precedence

The less than or equal to operator <= will evaluate true if the left operand is less than or equal to the right operand. JavaScript also contains greater than equal to >= operator.

console.log(10 <= 5);

Output:-

false

console.log(2 <= 2);

Output:-

true

Compare Variable & Value Using JavaScript less than or equal to (<=) Operator

const a = 2;
console.log(a <= 3); // true
console.log(a <= 2); // true
console.log(a <= 1); // false

Output:-

true
true
false

Compare 2 Variables Using JavaScript less than or equal to (<=) Operator

const a = 2;
const b = 3;
const c = 1;
console.log(a <= b); // true
console.log(a <= a); // true
console.log(a <= c); // false

Output:-

true
true
false

JavaScript less than or equal to (<=) operator also can be used to compare two strings alphabetically. Similarly, it can compare two characters also.

Compare Two Characters Using JavaScript less than or equal to (<=) Operator

console.log('a' <= 'a');
console.log('b' <= 'a');

Output:-

true
false

Compare Two Strings Using JavaScript less than or equal to (<=) Operator

console.log("Hello" <= "Hi");
console.log("Python" <= "Java");
console.log("React" <= "React");

Output:-

true
false
true

Compare bigint to number Using JavaScript less than or equal to (<=) Operator

We can also compare bigint to number. Note that bigint is not supported in all browsers.

console.log(5n <= 2);

Output:-

false

JavaScript also contains greater than or equal to >= operator which works very opposite to the less than or equal to <= operator.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *