Data type conversion is a big part of any programming language and converts String to Integer is one of the most used conversion.

Convert String to Int in JavaScript

To convert a string to integer in JavaScript, use the parseInt() method. The parseInt() method returns Nan, which means not a number when the string doesn’t hold a number. If the string with a number is sent, then only that number will be returned as an output.

The parseInt() method does not accept spaces. If any specific number with spaces is sent, then the part of a number that presents before space will be returned as an output.

Syntax

parseInt(value, radix);

Parameters

The **parseInt() **function takes a **value **as a **string **and **converts **it into an integer.

The radix parameter is used to define which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that a number in the string should be parsed from the hexadecimal number to the decimal number.

If the radix parameter is not passed, then JavaScript assumes the following:

  1. If a string begins with “0x”, the radix is 16 (hexadecimal).
  2. If a string begins with “0”, the radix is 8 (octal). This feature is deprecated.
  3. If a string begins with any other value, the radix is 10 (decimal).

#javascript #developer

How to Convert String to Int in Javascript
2.10 GEEK