javascript
Convert a String to Number or Integer in Javascript
const str = "200";
var num = parseInt(str);
If you add a number with a string format integer, it will not perfom sum operation instead it will apppend after the integer which is going to add with it. You need to convert that to integer format to make it functional. You can use parseInt() method to convert a string value to integer or number value. The syntax for that is as below.
Syntax
parseInt(string, radix)
It takes two parameters. First parameter string is required and second parameter radix is optional
string: The string parameter is a required parameter and in this you pass the value that needs to be converted to integer type.
radix: The radix is an optional parameter and it is passed as a number from 2 to 36 to indicate which numeral system it must use.
Live Demo of Integer type conversion
Was this helpful?
Similar Posts
- Convert a Number or Integer to String in Javascript
- How would you check if a number is an integer?
- Convert a string to array using Array.from() in javascript
- Join array items and convert to string in javascript
- Calculate square root of a number using javascript
- Count the total number of characters in a textarea while typing in javascript
- Javascript to get the number of days between two dates