</>
Andy Sciro

AndySciro

— Notes on Javascript & the web

The spread operator… it works on strings too

The spread operator… it works on strings too

I use the spread operator all day, every day like it’s going out of style. It was such a headslapping moment when I realized it can work on strings too — not sure if useful, but still pretty cool.

One thing I didn’t know was it’s not just for arrays and objects. Check out the Spread syntax (…) docs on MDN — how did I miss it?

So what does MDN say?

“Spread syntax (…) allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.”

So that means you can do something like this:

const str = "hello";
const chars = [...str];
// ["h", "e", "l", "l", "o"]

I’m not sure when I’ll actually need to use this over split(), but it’s still cool to know about it.