I’ve only been trying it in Firefox’s JavaScript console, but neither of the following statements return true:
parseFloat(\'geoff\') == NaN;
parseFloat(\'ge
While @chiborg 's answer IS correct, there is more to it that should be noted:
parseFloat('1.2geoff'); // => 1.2
isNaN(parseFloat('1.2geoff')); // => false
isNaN(parseFloat('.2geoff')); // => false
isNaN(parseFloat('geoff')); // => true
Point being, if you're using this method for validation of input, the result will be rather liberal.
So, yes you can use parseFloat(string)
(or in the case of full numbers parseInt(string, radix)
' and then subsequently wrap that with isNaN()
, but be aware of the gotcha with numbers intertwined with additional non-numeric characters.
I wrote this answer to another question on StackOverflow where another checks when NaN == null
but then it was marked as duplicate so I don't want to waste my job.
Look at Mozilla Developer Network about NaN
.
Just use distance || 0
when you want to be sure you value is a proper number or isNaN()
to check it.
The NaN (Not-a-Number) is a weirdo Global Object in javascript frequently returned when some mathematical operation failed.
You wanted to check if NaN == null
which results false
. Hovewer even NaN == NaN
results with false
.
A Simple way to find out if variable is NaN
is an global function isNaN()
.
Another is x !== x
which is only true when x is NaN. (thanks for remind to @raphael-schweikert)
Let's find out.
When you call NaN == false
the result is false
, same with NaN == true
.
Somewhere in specifications JavaScript has an record with always false values, which includes:
NaN
- Not-a-Number""
- empty stringfalse
- a boolean falsenull
- null objectundefined
- undefined variables0
- numerical 0, including +0 and -0I've created this little function that works like a charm. Instead of checking for NaN which seems to be counter intuitive, you check for a number. I'm pretty sure I am not the first to do it this way, but I thought i'd share.
function isNum(val){
var absVal = Math.abs(val);
var retval = false;
if((absVal-absVal) == 0){
retval = true
}
return retval;
}
Another solution is mentioned in MDN's parseFloat page
It provides a filter function to do strict parsing
var filterFloat = function (value) {
if(/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/
.test(value))
return Number(value);
return NaN;
}
console.log(filterFloat('421')); // 421
console.log(filterFloat('-421')); // -421
console.log(filterFloat('+421')); // 421
console.log(filterFloat('Infinity')); // Infinity
console.log(filterFloat('1.61803398875')); // 1.61803398875
console.log(filterFloat('421e+0')); // NaN
console.log(filterFloat('421hop')); // NaN
console.log(filterFloat('hop1.61803398875')); // NaN
And then you can use isNaN
to check if it is NaN
I just want to share another alternative, it's not necessarily better than others here, but I think it's worth looking at:
function customIsNaN(x) { return (typeof x == 'number' && x != 0 && !x); }
The logic behind this is that every number except 0
and NaN
are cast to true
.
I've done a quick test, and it performs as good as Number.isNaN
and as checking against itself for false. All three perform better than isNan
The results
customIsNaN(NaN); // true
customIsNaN(0/0); // true
customIsNaN(+new Date('?')); // true
customIsNaN(0); // false
customIsNaN(false); // false
customIsNaN(null); // false
customIsNaN(undefined); // false
customIsNaN({}); // false
customIsNaN(''); // false
May become useful if you want to avoid the broken isNaN
function.
Maybe also this:
function isNaNCustom(value){
return value.toString() === 'NaN' &&
typeof value !== 'string' &&
typeof value === 'number'
}