1

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);
}
David542
  • 104,438
  • 178
  • 489
  • 842
  • 2
    `Number("2xa")` will correctly give you `NaN`, and even simpler, `+"2xa"`. The `parseFloat()` and `parseInt()` functions were (my theory here) intended for parsing the numeric part of style properties like "width" and "height" values. – Pointy May 27 '23 at 02:54
  • @Pointy -- oh perfect, that is what I was looking for, and `+` is a simple way to do it. Out of curiosity, why do both `parseFloat` and `parseInt` allow a different type of input than `Number` does? – David542 May 27 '23 at 02:55
  • 1
    Like I said, when looking at the string value of a style property like "width", it might be "25px". The `parseInt()` function will give you 25 and ignore the "px" part. Now, that's just my theory, the real reason could be different. Those are old functions from original JavaScript in the 90s. – Pointy May 27 '23 at 02:57
  • 2
    Or use type coercion. `a == parseFloat(a)` does what you need it to do provided you explicitly use `==` and _not_ `===` (this is basically the only time where `==` makes sense) – Mike 'Pomax' Kamermans May 27 '23 at 03:06
  • `Number` allows its own set of potentially weird things (`Infinity`, `.123e+4`, whitespace). A regex is a perfectly good solution for stating explicitly what you want. – Ry- May 27 '23 at 03:20
  • Yes, if you want something different than the JavaScript number syntax (which allows leading `.` without an integer part) a regex is very flexible. – Pointy May 27 '23 at 03:28
  • @Mike'Pomax'Kamermans comparing to `null` is also (to me) a good use of `==` because it also handles `undefined`, which in many situations are essentially the same. – Pointy May 27 '23 at 03:29
  • 1
    Does this answer your question? [Check whether variable is number or string in JavaScript](https://stackoverflow.com/questions/1303646/check-whether-variable-is-number-or-string-in-javascript) – Camilo May 27 '23 at 03:38
  • @Pointy modern JS has the nullish operator `??` and nullish assignment `??=` for that, so no need for `==` there anymore. – Mike 'Pomax' Kamermans May 27 '23 at 04:02
  • @Mike'Pomax'Kamermans yea that's true, I haven't gotten super-used to those yet. – Pointy May 27 '23 at 04:16
  • @Mike'Pomax'Kamermans: Those replace `x == null ? undefined : …`, but not `x == null` in general. – Ry- May 27 '23 at 05:03

1 Answers1

1

Javascript doesn't have strict built in function that check if string input is a number or not but you can use Number() and isNaN() simultaneously like :

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 res = isNaN(Number(val)) ? null : Number(val);
  console.log(res === expected ? 'OK' : 'ERROR', res);
}

Here Number() function is used to convert string to number even we pass integer or float as form of string it will convert it into number and if we pass string other than number than it will return NaN .

Here isNaN() is an additional check if Number() return Not-A-Number.

Jagroop
  • 1,777
  • 1
  • 6
  • 20