Search code examples
javascriptparseintparsefloat

parseInt('1e1') vs parseFloat('1e1')


parseInt(1e1); //10
parseInt('1e1'); //1
parserFloat('1e1') //10

Why parseInt returns 1 in the second case? The three shouldn't return the same result?


Solution

    1. 1e1 is a number literal that evaluates to 10; parseInt() sees 10 and happily returns that.
    2. '1e1' is a string, and parseInt() does not recognize exponential notation, so it stops at the first letter.
    3. '1e1' as a string is perfectly fine when parsed as a float.

    Bonus: parseInt('1e1', 16) returns 481, parsing it as a 3-digit hex number.