I have a requirement to convert from String to the multiple other data types dynamically in Javascript. The datatype name is stored in a variable.
For example
let dt = 'Integer';
let toConvert = '1';
//I want to convert the 'toConvert' string to an Integer
How do i do this dynamically as dt can change based on scenarios.
So what you probably want to do my guy is use the parseint method. This will take a string variable and return a number.
const string = '10s123dqwd'
console.log(parseInt(string))
//or you could write
const number = parseInt(string)
console.log(number
//output = 10123
This method also removes any chars from the string as well.
If you are interested in reading more on this topic there is an article which takes an in depth look at multiple methods of converting java script strings into numbers here https://dev.to/sanchithasr/7-ways-to-convert-a-string-to-number-in-javascript-4l