Skip to content
Advertisement

How to get character array from a string?

How do you convert 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','!']

Advertisement

Answer

Note: This is not unicode compliant. "I💖U".split('') results in the 4 character array ["I", "�", "�", "u"] which can lead to dangerous bugs. See answers below for safe alternatives.

Just split it by an empty string.

var output = "Hello world!".split('');
console.log(output);

See the String.prototype.split() MDN docs.

Advertisement