Syntax, Parameter and Return Value
Syntax:
_.gte(value, other)
Parameters:
-
value: The value to compare.
- other: The other value to compare against.
Return Value:
(boolean) - Returns true if value is greater than or equal to other, else false.
Examples
Basic Numerical Comparison:
JavaScript
var _ = require('lodash');
console.log(_.gte(3, 3));
console.log(_.gte(3, 4));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
false
Demonstrates the basic usage of _.gte() for comparing numbers.
Using in Conditional Statements:
JavaScript
var score = 75;
if (_.gte(score, 60)) {
console.log('Passed');
} else {
console.log('Failed');
}

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Passed'
Shows how to use _.gte() in a conditional statement for decision-making.
Range Checking:
JavaScript
var age = 18;
if (_.gte(age, 18)) {
console.log('Eligible');
} else {
console.log('Not Eligible');
}

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Eligible'
An example of using _.gte() for checking eligibility based on age.
Sorting Array Elements:
JavaScript
var numbers = [5, 3, 4, 1, 2];
var sortedNumbers = _.sortBy(numbers, (a, b) => _.gte(a, b));
console.log(sortedNumbers);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 2, 3, 4, 5]
Demonstrates using _.gte() in sorting an array of numbers.
Frequently Asked Questions
Can _.gte() be used with non-numeric values?
While _.gte() is primarily designed for numerical comparisons, it can technically be used with any types that are comparable in JavaScript. However, it's most meaningful and reliable with numbers.
How does _.gte() handle NaN values?
When comparing with NaN (Not a Number), _.gte() follows JavaScript's comparison rules, and the result will typically be false.
Is _.gte() suitable for array or object comparisons?
_.gte() is not intended for direct comparisons of arrays or objects. It is best used for simple values like numbers or strings, where the concept of "greater than or equal to" is clearly defined.
Conclusion
Lodash's _.gte() method offers an effective and straightforward way to perform greater-than-or-equal-to comparisons. It is a valuable tool for numerical comparisons, making conditional logic and range checks more expressive and easier to understand.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.