My question is why this function can not be simplified to:
function isNumeric(n) {
return isFinite(n)
}
It can't, no, you want that parseFloat in there if there's any chance n may not be a number to start with. But it could be simplified to:
function isNumeric(n) {
return isFinite(parseFloat(n));
}
...because isFinite(NaN) is false, which is what your function returned for unparseable numbers anyway.
It's worth noting that parseFloat will stop at the first invalid character and return whatever number it found up to that point, so parseFloat("123abc") is 123, not NaN. If you want to validate that the entire string can be correctly parsed as floating point, use + or Number():
function isNumeric(n) {
return isFinite(+n); // Or: return isFinite(Number(n));
}
Now, if you know for sure that n will always already be a number (not a boolean or a string or something), then yes, you could skip the parsing stage.