Convert string to character array in JavaScript
Question:
How to convert string to character array in JavaScript? Answer:
function toCharArray(str) {
return [...str];
}
console.log(toCharArray('this is my text'));
console.log(toCharArray('Spec éáőú and 💡✨'));
Description:
By default, if you want to convert a string into a character array in vanilla JavaScript you maybe think of the split
function at first. Like this:
str.split('');
However, when using an empty string as a separator the split function uses UTF-16 code units and this ruins Unicode characters built up from 2 elements, for example:
console.log('Spec text 💡✨'.split(''));
Gives the result:
['S','p','e','c',' ','t','e','x','t',' ','\uD83D','\uDCA1','✨']
Solution
A better and even simpler solution is to simply use an array spread operator like this:
console.log([...'Spec text 💡✨']);
where the result will be the expected:
['S','p','e','c',' ','t','e','x','t',' ','💡','✨']
So that's a simple way to convert a string into a character array in modern JavaScript.
Reference:
String split reference
Share "How to convert string to character array in JavaScript?"
Related snippets:
- Remove double quotes from a string in JavaScript
- Check if date is valid in JavaScript
- Check if a string is empty in JavaScript
- Check if string contains a substring in JavaScript
- Escape quotes in JavaScript
- Count the number of characters in a string with JavaScript
- Concatenate strings in JavaScript
- Check if string ends with slash using JavaScript
- Get the last character of a string in JavaScript
- Convert array to comma separated string using JavaScript
- Remove all extra spaces from a string in JavaScript
- Check if string contains a specific character using JavaScript
- Remove spaces from string at the beginning, end, or both in JavaScript
- Remove protocol from URL with JavaScript
- Remove emojis from a string with JavaScript
- Check if a string ends with number in JavaScript
- Remove specific character from a string in JavaScript
- Remove the last character from a string in JavaScript
- Count the number of words in a string using JavaScript
- Count the number of lines in a string in JavaScript
- Convert string to int in JavaScript
- Convert string to lowercase with JavaScript
- Generate random string in JavaScript
- Generate UUID in JavaScript
- Generate random color with JavaScript
- Convert string to kebab case in JavaScript
- Convert snake case to camel case in JavaScript
- Capitalize words in a string using JavaScript
- Convert string to character array in JavaScript
- Convert camel case to snake case in JavaScript
- Remove accents from a string in JavaScript
- Convert a string to sentence case in JavaScript
- Convert string to title case in JavaScript
- Convert string to snake case in JavaScript
- Convert string to pascal case in JavaScript
- Convert string to camel case in JavaScript
- Convert string to uppercase with JavaScript
- Convert object to JSON string in JavaScript
- Parse JSON string in JavaScript
- convert number to string in JavaScript
- Use colors in JavaScript console
- Get string length in JavaScript
- Split a string into words
- Replace line break with br in JavaScript
- Replace spaces with dashes in JavaScript
- Create multiline string in JavaScript
Tags:
string, character, array, convert, unicode, javascript, spread Technical term:
Convert string to character array in JavaScript