Greater than and less than operators in JavaScript
To check which value is greater and which
is less, the operators greater than >,
greater than or equal to >=, less
than <, less than or equal to
<= are used.
Let's study their work on a practical
example. Suppose we have the variable
test with some value:
let test = 1;
Let's check if the value of this variable is greater than zero or not:
let test = 1;
if (test > 0) {
console.log('+++'); // it will work
} else {
console.log('---');
}
Now let's change the value of the variable to negative:
let test = -1;
if (test > 0) {
console.log('+++');
} else {
console.log('---'); // it will work
}
Now let the value of the variable be
equal to 0. In this case, we
will get into the else block,
since our condition says that the variable
test must be strictly greater than zero:
let test = 0;
if (test > 0) {
console.log('+++');
} else {
console.log('---'); // it will work
}
Let's change the condition to greater than or equal to:
let test = 0;
if (test >= 0) {
console.log('+++'); // it will work
} else {
console.log('---');
}
And now to less than:
let test = 0;
if (test < 0) {
console.log('+++');
} else {
console.log('---'); // it will work
}
And now to less than or equal to:
let test = 0;
if (test <= 0) {
console.log('+++'); // it will work
} else {
console.log('---');
}
Check if the variable test
is greater than 10.
Check if the variable test
is less than 10.
Check if the variable test
is greater than or equal to 10.
Check if the variable test
is less than or equal to 10.