Javascript parseFloat Replacement - TechRepublic
General discussion
September 24, 2003 at 09:29 AM
mathguy7

Javascript parseFloat Replacement

by mathguy7 . Updated 22 years, 9 months ago

Hello.

I am looking for information regarding a Javascript action that I only learned about yesterday.

Up to now, I have been using parseFloat to get data from a form. For example, I might have the following line:

tempx = parseFloat(document.forms[0].elements[xIndex].value);

However, somebody told me that a better string-to-number type conversion method is the unary plus operator (+). It is said to be about 4 times faster than parseFloat and still produces NaN results if the string cannot be converted into a number (for checking to ensure a valid number is input).

So my above line would become:

tempx = (+document.forms[0].elements[xIndex].value);

Can somebody please let me know more about this feature, direct me to some documentation, etc.

Also, when using EITHER of these functions to input numbers FROM A FORM, is it best practice to explicitly check that the input is valid? Or will both these functions return NaN for invalid input anyhow?

Any help would be appreciated.

Thank-you.

This discussion is locked

All Comments