1
let string = "13th"

string = Number(string)

I expect it to return 13 instead I get NaN please how do I go about it

Chukwuemeka Maduekwe
  • 6,687
  • 5
  • 44
  • 67

1 Answers1

7

Try with parseInt()

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

let string = "13th";

string = parseInt(string);
console.log(string);
Mamun
  • 66,969
  • 9
  • 47
  • 59