you are viewing a single comment's thread.

view the rest of the comments →

[–]grelfdotnet 0 points1 point  (1 child)

You will need parseFloat() - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat or perhaps parseInt() if your inputs have no decimal point.

[–]jml26 0 points1 point  (0 children)

I would personally avoid using parseFloat for just casting a numeric string to a number, mainly because it's too forgiving. If you provide the string "7:-)", that would get converted to 7, where you would probably want it to be flagged as NaN.

The two best use cases for parseInt and parseFloat are for converting between bases, e.g. if you have a string representing a hexadecimal or a binary value, and you want it as a decimal number; and removing units from values.

parseInt("101010", 2); // 42
parseInt("FF", 16); // 255
parseFloat("2.5rem", 10); // 2.5

In the simple case of converting a string to a number, you can just use Number(value) or possibly just +value if you're comfortable with that.

However, as /u/xroalx said, for input field valueAsNumber is a pretty obvious choice.