-2
const str = "+385(0)21386644";
const toNumb = Number(str)

console.log(toNumb) //NaN

This traditional way is not working :s

How can i convert this string to number?

  • 1
    remove non numeric characters – Bravo Jul 25 '21 at 09:37
  • 1
    Does this answer your question? [Strip all non-numeric characters from string in JavaScript](https://stackoverflow.com/questions/1862130/strip-all-non-numeric-characters-from-string-in-javascript) Research is an important step in developing – Mihai T Jul 25 '21 at 09:38
  • 3
    That doesn't look like something that should be converted to a number. It looks like a phone number located in Croatia (+385). From outside Croatia it would be whatever you dial for an international line (00 in the UK, 011 in the U.S., etc.), then 385 for Croatia, then 21386644. From within Croatia it would be 021386644. This is not a number. (Despite the name, phone numbers are not numbers and shouldn't be treated like them.) – T.J. Crowder Jul 25 '21 at 09:42

1 Answers1

3

Use String replace , to replace all non numeric characters with '' , then use Number

const x = Number("+385(0)21386644".replace(/[^0-9]/g, ''));
console.log(typeof x)
brk
  • 48,835
  • 10
  • 56
  • 78
  • There's a [well-established canonical](https://stackoverflow.com/questions/1862130/strip-all-non-numeric-characters-from-string-in-javascript) for this. But really, the OP shouldn't be converting this to a number in the first place, most likely. :-) – T.J. Crowder Jul 25 '21 at 09:44