JavaScript for WordPress Forums Vanilla JavaScript 1.2.06 – Data Types – Numbers & NaN – parseInt vs parseFloat vs Number

  • This topic has 2 replies, 2 voices, and was last updated 6 years ago by Adam.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #15142
    Adam
    Participant

    I just finished 1.2.06 – Data Types – Numbers & NaN

    Maybe I’m getting ahead of myself here… but I’m a little confused by the distinction between parseInt vs parseFloat, specifically the need for the distinction. I understand that parseInt is for whole numbers and parseFloat is for numbers with decimal points. What I don’t understand is why we need to have two different functions, especially when the Numbers function also exists and doesn’t distinguish between integers or decimal points. The only thing I could think of would be if a number was entered or computed which would result in a decimal point, but only a whole number would be needed moving forward? I did a few Google searches and all I found was far more advanced than where I’ve gotten so far in this course (radix, octal, etc). Any insight would be appreciated–thanks!

    #16864
    Zac Gordon
    Keymaster

    You are right!

    I am not sure of the specific reasons, except that the creator of JS thought it would be helpful to include both of these functions. I imagine the influence of strongly typed languages and really wanted to clear what type of number you would get back was part of it.

    You’re right the differences you pointed out are quite technical and not something you probably need to really understand at this point.

    Somethings like different index points though should make sense as you move into arrays http://stackoverflow.com/questions/9528433/behavior-difference-between-parseint-and-parsefloat

    Hope that helps? Great question!!!

    #16899
    Adam
    Participant

    Thanks–that’s perfect 🙂

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.