Course contentsShow
← JavaScript
Lesson 191 of 2,7235. StringsFree lesson

Spread Operator and Array.from for String Splitting

Convert strings to arrays of characters using spread or Array.from, preserving multi-byte characters correctly.

Spread Operator and Array.from for String Splitting

What you'll learn: How to convert strings into arrays of individual characters using two modern techniques that correctly handle multi-byte Unicode characters.

The Problem with Traditional Splitting

You already know split('') can break a string into characters, but there's a catch: it doesn't properly handle surrogate pairs (like emojis or rare characters). Those multi-byte characters get split incorrectly, creating broken halves instead of complete characters.

Two Better Solutions

The spread operator (...) and Array.from() both treat strings as iterable sequences of code points, not code units. This means they respect character boundaries, even for emojis and other complex Unicode symbols.

Using the Spread Operator

const text = "Hello 👋";

// Old way - breaks emojis
const broken = text.split(''); 
// ['H','e','l','l','o',' ','�','�']  ← Oops!

// Modern way - preserves emojis
const correct = [...text];
// ['H','e','l','l','o',' ','👋']  ← Perfect!

The spread operator unpacks the string character-by-character, respecting Unicode boundaries.

Using Array.from()

const text = "café 🎉";

const chars = Array.from(text);
// ['c','a','f','é',' ','🎉']

Array.from() does the same thing: it iterates through the string properly, treating each visual character as one element.

Think of It This Way

Imagine a string is like a necklace with beads. Some beads are simple (like 'A'), but others are complex charms that take up two links (like '👋'). The spread operator and Array.from() see each bead as a unit, while old methods sometimes saw individual links, breaking those fancy charms in half.

Key Takeaway: Use [...string] or Array.from(string) instead of split('') to safely convert strings to character arrays while preserving all Unicode characters, including emojis and special symbols.