A very basic example of parseInt might involve just passing a string of a number as the first argument when calling the parseInt function. The returned result will be a number rather than a string and if all goes well the value of the number will be the same of that of the string value. When called parseInt will attempt to parse what is passed to it as a number that will be an integer value, so I do not need to bother with rounding the result, more on rounding a little later So if for example I pass the string value of 42 the result will be a number with the value of 42 rather than a string of that number, also if I give a string value like 42.125 the result will again be f2
There is the question of how parseInt will treat a fraction of a number, when it comes to the subject of rounding, or just cutting the fraction part of a number. With that said it would seem that parseInt will just cut the fraction part from the number value. If this is a problem then this would then be a reason why one would prefer to use one of the options for rounding numbers in place of using parseInt. Such as using the parseFloat method and then the Math.ceil, Math.round, or Math.floor methods.
There are a few reason why one might end up with a NaN value when using parseInt. One such reason why NaN might be the result is when it comes to strings that contain letters that are to be used with a certain radix, NaN can result if the proper radix is not given. For example say I have a string that contains a number but the radix is not 10, but 16, in other words a hex number. If I just pass the hex string to parseInt the result will be NaN because the default radix of 10 is what was used. If I pass the hex string as the first argument, and then set the proper radix of 16 as the second argument, then the desired number value will be returned.
Be mindful of any characters that are not used at all for number values of any radix. If a char that is not part of a number is at then end of a string then the parseInt method will just ignore it, and work with any valid chars from the start of the string up to that index in the string. However if a string begins with a char that is not used even with the property radix for the rest of the values that will result in NaN. The parseInt method will not preform any kind of text pattern matching for you, you will need to do that before hand when it comes to extracting the desired input value for the parseInt function.
The parseInt function will work just fine in most cases, however thus far I can not say that I use it often. I am not saying that using the parseInt method is bad practice it is just that there are alternatives that also work well. Also in some cases the alternatives will work better in some of those cases where the parseInt method will fall short, such as with using numbers that go into notation. Also I often like having a higher degree of control when it comes to how to handle the fraction part of a source string or number.
The Number function can be used to convert a string to a number also. It is a way to explicitly declare the the value that is given to the number function is th be parsed as a number. However it will not parse to an integer, at least not my itself, so it would have to be used in conjunction with an additional method such as the Math.round method.
When it comes to making any kind of object by way of creating a constructor function, or just a plain object literal I can define what a valueOf method should be for this kind of object. The return value of a valueOf method should be whatever the primitive value of the object should be. So then in the body of this valueOf method I can make it so that the primitive value that is returned is a number, and by any means make sure that the number is an integer.
There is also the parseFloat function that will parse a string value into a number with the fraction part of the number. There is doing that, and then preforming any additional things that I want to do when it comes to rounding or cutting the decimal.
The parseInt method can be used to get an int value, but I still find myself using the Number function along with the built in Math object rounding methods combined with other methods to insure a number rather than a string. In many cases this just allows for a better degree of control of the outcome of a number. For example I get to chose what method I will use to round a number rather than just having the fraction value cut off. I can get the same effect by using the Math.floor method if I want, but in some cases I might want to use one of the other rounding methods.