I’m having a problem when executing parseFloat() – I don’t understand why it produces the following outputs:
document.write(parseFloat("6e2") + "<br />"); //output is 600 why?
document.write(parseFloat("6b2") + "<br />"); //output is 6 why?
document.write(parseFloat("6c2") + "<br />"); //output is 6 why?
Could you tell me how the script is working?
6e2produces 600 because it’s treating your input as scientific notation.6e2 == 6 x 102 == 600
The other two produce 6 because
parseFloatparses the 6, then gets to input it isn’t able to convert to a number, so it stops, and returns the result found so far.Per MDN: