I would recommend using JavaScripts Infinity value because it is consistent.
For example:
var x = 1;
var y = 0;
var z = x / y;
console.log(z);
// results in `Infinity`
I would say null
is also another choice but this could be mistaken for no value where as Infinity
is actually a value of endless possibility.
Definitely don't use NaN
. It's a weird beast, and ES apologized for this anomaly.
Take this for example:
var x = NaN;
if (x === x) {
console.log('Good');
} else {
console.log('What?');
}
The above answer results in 'What?' which tells us NaN is in fact some sort of value that is not a number.
In addition, it's typeof
is a number
. You can tell if it is positive infinity or negative. If you are working with numbers, use Infinity
. It is consistently equal to Infinity
and will be the best solution.
var x = Math.log(0);
console.log(x);
console.log(typeof(x));
if (x === x) {
console.log('yes');
}
Update
From HERE - You can use:
{ a: { is_infinity: true, value: null }, b: { is_infinity: false, value: 10 } }
null
indeed sounds like a bad idea - loosely typed languages like PHP will convert anull
to0
in some circumstances. – Pieplant