I want to convert a string variable as follows:
- the variable string is a float-> convert to float
- else string is an integer-> convert to integer
- else return string
This is what I currently tried:
function parse(x){
return x==parseFloat(x)?parseFloat(x):
x==parseInt(x)?parseInt(x):
x
}
console.log(typeof parse("11.5"),parse("11.5"))
console.log(typeof parse("11"),parse("11"))
console.log(typeof parse("11.5A"),parse("11.5A"))
I am looking for solutions if there exists a more efficient and direct way to do this.