JavaScript Less-than or Equal-to

JavaScript Less-than or Equal-to (<=) Comparison Operator is used to check if the first operand is less than or equal to the second operand. Less-than or Equal-to operator returns a boolean value. The return value is true if the first value is less than or equal to the second, else, the return vale is false.

Less-than or Equal-to Operator Symbol

The symbol used for Less-than or Equal-to Operator is <=.

Syntax

The syntax to use Less-than or Equal-to Operator with operands is

operand1 <= operand2

Each operand can be a value or a variable.

Since Less-than or Equal-to operator returns a boolean value, the above expression can be used as a condition in If-statement.

if (operand1 <= operand2) {
    //code
}

Examples

In the following example, we take two values in variables: x and y; and check if the value in x is less than or equal to that of in y.

index.html

<!DOCTYPE html>
<html lang="en">
<body>
    <pre id="output"></pre>
    <script>
        var x = 2;
        var y = 5;
        var result = x <= y;
        document.getElementById('output').innerHTML += 'x less than or equal to y ?  ' + result;
    </script>
</body>
</html>

Let us take same value in both x and y, and check the output.

index.html

<!DOCTYPE html>
<html lang="en">
<body>
    <pre id="output"></pre>
    <script>
        var x = 5;
        var y = 5;
        var result = x <= y;
        document.getElementById('output').innerHTML += 'x less than or equal to y ?  ' + result;
    </script>
</body>
</html>

In the following example, let us use Less-than or Equal-to operator as a condition in the If statement’s condition.

index.html

<!DOCTYPE html>
<html lang="en">
<body>
    <pre id="output"></pre>
    <script>
        var x = 'apple';
        var y = 'banana';
        if (x <= y) {
            displayOutput = 'x is less than or equal to y.';
        } else {
            displayOutput = 'x is not less than or equal to y.';
        }
        document.getElementById('output').innerHTML = displayOutput;
    </script>
</body>
</html>

Conclusion

In this JavaScript Tutorial, we learned about Less-than or Equal-to Comparison Operator, its syntax, and usage with examples.