Hey folks!
we can easily convert String into an array by using spread operator (...)
:
const string = 'Shivam Kapasia';
const array = [...string];
console.log(`${array}`); // S,h,i,v,a,m, ,K,a,p,a,s,i,a
Note : The array will also include space, but if you don't want to store space in array first remove space from string then use spread operator.
Whatβs your preferred way to convert string into an array ?
Top comments (5)
Depends really why you want to convert it? Since a string is already iterable, for a lot of use cases the conversion is not necessary
Agreed!
And all of array methods are generic and can be called on string with call or apply.
You could also use
str.split('')
Thanks for highlighting this, will definitely try with
Intl.Segmenter
.