Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To split a string of numbers in javascript, use string.split('') instead of string use your string variable name it will return array of string number character.

Today, I’m going to show you How do I split a string of numbers in javascript, as mentioned above, I’m going to create a sample string variable then I will use string.split('') method to split and we will also see convert array of string number character to number.

Let’s start today’s tutorial on how do you split a string of numbers in javascript.

Javascript split a string of numbers

Here, we will do

  1. Create a sample string variable with data.
  2. Use split() method to split string of numbers
  3. Convert array string element to number
  4. revert to array of numbers to number

Bonus Point 5. Handle “,” between string numbers

// Create a sample string variable with data.
let str = "803,384,39";
console.log(str)

// Handle "," between string numbers
str  = str.replace(/,/g, '')
console.log(str)

// Use `split()` method to split string of numbers
let arr = str.split("");
console.log(arr)

// Convert array string element to number
arr = arr.map((e) => +e)
console.log(arr)

// revert to array of numbers to number
let num = +arr.join().replace(/,/g, '')
console.log(num)

Output

I hope it helps you, All the best đź‘Ť.