Categories
JavaScript Answers

How to convert a string of numbers to an array of numbers?

Spread the love

To convert a string of numbers to an array of numbers, we use the split and map methods.

For instance, we write

const a = "1,2,3,4";
const b = a.split(",").map(Number);

to call split to split the a string into a string array by the commas.

And then we call map with Number to convert each entry to a number and return them in a new array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *