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','!']
Just split it by an empty string.
See
String.prototype.split()
MDN docs.Since this question is originally asked more than five years ago, people are still misopetating this type of task. As hippietrail suggests, meder's answer can break surrogate pairs and misinterpret “characters.” For example:
You do not need to do anything. It is already array of char.
You can also use
Array.from
.This method has been introduced in ES6.
Reference
Array.from
This is an old question but I came across another solution not yet listed.
You can use the Object.assign function to get the desired output:
Not necessarily right or wrong, just another option.
Object.assign is described well at the MDN site.
Without using any function: