Navigate back to the homepage

How do you split a string of numbers in Javascript?

Infinitbility
JavaScript
October 16th, 2022 · 1 min read

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

1// Create a sample string variable with data.
2let str = "803,384,39";
3console.log(str)
4
5// Handle "," between string numbers
6str = str.replace(/,/g, '')
7console.log(str)
8
9// Use `split()` method to split string of numbers
10let arr = str.split("");
11console.log(arr)
12
13// Convert array string element to number
14arr = arr.map((e) => +e)
15console.log(arr)
16
17// revert to array of numbers to number
18let num = +arr.join().replace(/,/g, '')
19console.log(num)

Output

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to get length of string Javascript?

An example of get length of string in javascript with the step-by-step guide.

October 15th, 2022 · 1 min read

How to divide two numbers in react js?

An example of divide two numbers in react js with the step-by-step guide.

October 14th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]