JavaScript Comparison Operators

JavaScript Comparison Operators are used to compare two values, like if they are equal or not equal, or greater than the other or less than the other, etc.

Comparison Operator – Symbol – Example

JavaScript supports the following Comparison Operators.

Comparison OperationOperator SymbolExample
Equal to==a == b
Equal Value and Equal Type===a === b
Not-equal!=a != b
Greater-than>a > b
Less-than<a < b
Greater-than or Equal-to>=a >= b
Less-than or Equal-to<=a <= b

Tutorials

The following JavaScript tutorials cover each of these Comparison Operators in detail.

Example

In the following example, we take two values, and perform different Comparison Operations on these values.

index.html

<!DOCTYPE html>
<html lang="en">
<body>
    <pre id="output"></pre>
    <script>
        var a = 7;
        var b = 4;
        var equal_to = (a == b);
        var equal_value_type = (a === b);
        var not_equal = (a != b);
        var greater_than = (a > b);
        var less_than = (a < b);
        var greater_or_equal = (a >= b);
        var less_or_equal = (a <= b);

        var displayOutput = '';
        displayOutput += '\n' + '(a == b) = ' +  equal_to;
        displayOutput += '\n' + '(a === b) = ' +  equal_value_type;
        displayOutput += '\n' + '(a != b) = ' +  not_equal;
        displayOutput += '\n' + '(a > b) = ' +  greater_than;
        displayOutput += '\n' + '(a < b) = ' +  less_than;
        displayOutput += '\n' + '(a >= b) = ' +  greater_or_equal;
        displayOutput += '\n' + '(a <= b) = ' +  less_or_equal;
        document.getElementById('output').innerHTML = displayOutput;
    </script>
</body>
</html>

Conclusion

In this JavaScript Tutorial, we learned about Comparison Operators, their syntax, and usage with examples.