Function parseFloatX

  • There are a lot of ways to convert a string to a number in JavaScript. And they are all slightly different!

    This is my preferred way to parse a number. Any errors are reported as undefined, so you can choose to get rid of them with ??.

    I get rid of NaNs and infinities. I don't think I every really send an infinity over the network or save it in a file. These become undefined, just like errors.

    Parameters

    • source: undefined | null | string

      The input to parse.

    Returns number | undefined

    A finite number or undefined if the parse failed.