What is the difference between parseInt() and number()?

parseInt() and Number() are both used to convert a string into a number.

  • parseInt() parses the value of the string and converts to number till the first non-digit character.
  • Number() tries to convert the type of the string to number and returns nothing if there are non-digit characters in the string.

Example:

parseInt("67dpi");   // returns 67

// Convert type
Number('67dpi');     // returns NaN

parseInt() and Number() are both used to convert a string into a number.

  • parseInt() parses the value of the string and converts to number till the first non-digit character.
  • Number() tries to convert the type of the string to number and returns nothing if there are non-digit characters in the string.

Example:

parseInt("67dpi");   // returns 67

// Convert type
Number('67dpi');     // returns NaN
Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *