Sometimes, we want to convert string into array of integers with JavaScript.
In this article, we’ll look at how to convert string into array of integers with JavaScript.
How to convert string into array of integers with JavaScript?
To convert string into array of integers with JavaScript, we can use the string split
and array map
methods.
For instance, we write
const arr = "14 2".split(" ").map(Number);
to call split
with ' '
to split the string by the spaces into an array of substrings.
Then we call map
with Number
to convert each substring into a number and return a new array with the numbers.
Conclusion
To convert string into array of integers with JavaScript, we can use the string split
and array map
methods.