Categories
JavaScript Answers

How to cast a string to an array with JavaScript?

Spread the love

Sometimes, we want to cast a string to an array with JavaScript.

In this article, we’ll look at how to cast a string to an array with JavaScript.

How to cast a string to an array with JavaScript?

To cast a string to an array with JavaScript, we can use the spread operator.

For instance, we write:

const s = 'foo'
const arr = [...s]
console.log(arr)

to spread the characters of s into an array with the spread operator and assign the array to arr.

Therefore, arr is ['f', 'o', 'o'].

Conclusion

To cast a string to an array with JavaScript, we can use the spread operator.

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 *