Convert a String to an Array
suggest changeThe .split() method splits a string into an array of substrings. Called with no separator, it returns a one-element array containing the whole string. It does not split on spaces unless you pass " " or /\s/.
The argument is the string or regular expression to split on.
To split into individual UTF-16 code units, call .split with an empty string (""). Characters outside the Basic Multilingual Plane, including many emoji, are encoded as two code units, so split("") separates those halves. There is no slice("") that splits by code point. The string iterator, used by spread below, yields code points instead.
const strArray = "StackOverflow".split("");
// strArray = ["S", "t", "a", "c", "k", "O", "v", "e", "r", "f", "l", "o", "w"]
Using the spread operator (...), to convert a string into an array.
const strArray = [..."sky is blue"];
// strArray = ["s", "k", "y", " ", "i", "s", " ", "b", "l", "u", "e"]
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents