How do you get a string to a character array in JavaScript?
I'm thinking getting a string like "Hello world!"
to the array ['H','e','l','l','o',' ','w','o','r','l','d','!']
How do you get a string to a character array in JavaScript?
I'm thinking getting a string like "Hello world!"
to the array ['H','e','l','l','o',' ','w','o','r','l','d','!']
The
spread
SyntaxYou can use the spread syntax, an Array Initializer introduced in ECMAScript 2015 (ES6) standard:
Examples
The first three result in:
The last one results in
Browser Support
Check the ECMAScript ES6 compatibility table.
Further reading
spread
is also referenced as "splat
" (e.g. in PHP or Ruby or as "scatter
" (e.g. in Python).Demo
Try before buy
It already is:
Or for a more older browser friendly version, use: