length, spread, and split("")
string.length
[...string]
Array.from(string)
string.split("")Choose a code-point iteration method when you need character counts or when you split user-facing text. Do not use split("") for that job.
- codePointAt(0) on a string that starts with 👍 returns 0x1f44d, the full code point.
- JSON.stringify shows isolated surrogates as � and � escapes, which is useful in tests.
const text = "👍";
console.log(text.length);
console.log([...text].length);
console.log(JSON.stringify(text.split("")));
console.log(JSON.stringify(Array.from(text)));Expected output
2
1
["\ud83d","\udc4d"]
["👍"]