JavaScript – Greater-than (>)
JavaScript Greater-than (>) Comparison Operator is used to check if the first operand is greater than the second operand. Greater-than operator returns a boolean value. The return value is true if the first value is greater than the second, else, the return vale is false.
Greater-than Operator Symbol
The symbol used for Greater-than Operator is >
.
Syntax
The syntax to use Greater-than Operator with operands is
operand1 > operand2
Each operand can be a value or a variable.
Since Greater-than 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 greater than that of in y
using Greater-than Operator.
index.html
<!DOCTYPE html>
<html lang="en">
<body>
<pre id="output"></pre>
<script>
var x = 4;
var y = 2;
var result = x > y;
document.getElementById('output').innerHTML += 'x greater than y ? ' + result;
</script>
</body>
</html>
In the following example, let us use Greater-than 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 greater than y.';
} else {
displayOutput = 'x is not greater than y.';
}
document.getElementById('output').innerHTML = displayOutput;
</script>
</body>
</html>
Conclusion
In this JavaScript Tutorial, we learned about Greater-than Comparison Operator, its syntax, and usage with examples.