Categories
JavaScript Answers

How to convert string into array of integers with JavaScript?

Spread the love

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.

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 *