Is there a more strict built-in javascript function that will tell me if a string input is a number or not? For example:
// for both of the following it should produce a non-value (nan or undefined or whatever)
> parseFloat('2xa');
2
> parseInt('1ddd')
1
Currently I'm using a regex to do it:
const test_values = [
// [value, expected]
['2.4', 2.4],
['1e2', 1e2],
['1f', null],
['0.28', 0.28],
['1e+7', 1e+7],
['1e+7.1', null]
]
for (let [val, expected] of test_values) {
let m = val.match(/^(-?\d*(\.\d+)|\d+)([eE][+-]?\d*)?$/);
let res = m? parseFloat(m[0]) : null;
console.log(res === expected? 'OK' : 'ERROR', res);
}