To my knowledge there a few tricks that you can pull off with Infinity
A number larger than any number is actually pretty useful, as the negative sign and you negative number is then larger than any negative number.
Infinity;
-Infinity;
typeof Infinity; // number
// 1.
"Hello World".substr(4, Infinity);
// 2.
"Hello World".substr(4, -Infinity);
1 outputs o world
, usually that's a tiny bit harder, you need to know the length of the string do some bad thing like use an arbitrary large number such as 9e9
although unlikely, it will still fail to account for cases where the string was 9e9 + 1 long, I can't live with that...
2 outputs nothing, because well negative numbers in second position arg of substr
Okay so Infinity is pretty useful for ummm.... Not much let me know what you use it for, I know I have used it from time to time to simplify some problems π¬
Top comments (3)
I have used it for sortable NULL values. Do you want it NULLS FIRST or NULLS LAST? Both can be done.
Nulls first for sure π₯³
Do you have some code example?
Infinity can be really useful for implementing dijkstraβs algorithm, except I donβt really know why would would in JavaScript