Header Image Image by Me

The Curious Case of parseInt() function

|posted on : 15/06/2021

parseInt()

the parseInt() function simply parses a string argument into an integer within its specified radix in simple words it converts a string argument into numeric form (integer in this case) with a specified base( binary , octal , decimal ,hex etc.)

Syntax

parseInt(string)
//or
parseInt(string , radix)

    Things to note

  1. In the given code snippet “string” parameter is the value which is supposed to be parsed into an integer
  2. As mentioned in the snippet that use of base is optional if the user wants the default state of radix

Default Value

var hexInteger =parseInt('0xC') // starts with"0x" hence implicitly declares radix as 16
console.log(hexInteger) //output : 12

if you don’t specify a radix than the ‘parseInt()’ function will implicitly apply decimal base to it however if the string starts with digit 0 than octal base would be applied by default and if the string starts with 0x than hexadecimal base(as mentioned in the snippet above) is applied to the same thus it is a good practice to specify the radix while calling this function

How It Parses a Specified String

Example 1

var string = parseInt('100 this text will be ignored') // only 100 would be printed and the rest of the non-numeric text is ignored
console.log(string) //output 100

Example 2

var string = parseInt('The whole string will be ignored 200') // JS ignores the whole string if the first character of the string is non-numerical
console.log(string) // output: NaN

  • If a specific character doesn’t have a numeric value according to the specified radix than such character and all the upcoming numeric or non-numeric values are ignored that’s why if the very first character of the string is not identified as numeric than the whole string would be ignored and the ultimate output would be NaN.

  • If the whole string is non numeric in nature than again NaN would be returned as an output

Example 3

var string = parseInt('4 2 0 ') // JS would ignore any whitespace and anything superseding that
console.log(string) // output: 4

  • 'parseInt()' function ignores the empty whitespace and any consecutive character after that

Other Things to Note

  1. In cases where the string exceeds the limit of base or it is smaller/larger than the default base limits i.e. 2 and 36 than it will return NaN(Not a Number) as an output
  2. English alphabets represents numbers greater than 9 for Radices /Bases equal to or above or 10
  3. For heavy or large numbers ‘math.floor()’ function should be used instead of ‘parInt()’

Conclusion

Functions like 'parseInt()' or 'parseFloat()' can help in performing multiple tasks like converting the numerical input by user (which is string by default) to an integer/value which may further help you to do certain calculations or even apply algorithms based on that e.g:- a web app which converts number of minutes into hours and vice-versa based on user input.


©Shubhendu Sen