Infinity in JavaScript
JavaScript's Infinity object roughly represents a value larger than any reachable value in JavaScript. It is rarely encountered, but still useful to know about since invalid mathematical calculation results may not always return NaN. Additionally, Infinity will not show up in isNaN error checking:
var divide = function(x, y) { return x/y; } var validOr0 = function(callback) { var result = callback(); if (isNaN(result)) { return 0; } return result; } console.log(validOr0(function() { return divide(4, 2); })); // 2 console.log(validOr0(function() { return divide(1, 'not number'); })); // 0 console.log(validOr0(function() { return divide(1, 0); })); // Infinity
To ensure the above performs more reliably, isFinite can be used:
var validOr0 = function(callback) { var result = callback(); if (isNaN(result) || !isFinite(result)) { return 0; } return result; } console.log(validOr0(function() { return divide(1, 0); })); // 0
Infinity can be in the negative or positive forms, so sometimes it makes sense to use the object directly the type of non-finiteness matters. But beyond that its usefulness seems to be limited. The only valid use case I could find was for speeding up calculations. Calling isFinite() is slower than comparing against Infinity values directly: http://stackoverflow.com/questions/190852/how-can-i-get-file-extensions-with-javascript/12900504#12900504
Github Location: https://github.com/Jacob-Friesen/obscurejs/blob/master/2017/infinity.js














