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.