# Breaking into letters with the spread operator!

**URL:** https://forum.kirupa.com/t/breaking-into-letters-with-the-spread-operator/639100
**Category:** web dev
**Created:** [December 10, 2018, 5:33am UTC](https://forum.kirupa.com/t/breaking-into-letters-with-the-spread-operator/639100 "2018-12-10T05:33:40Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [December 10, 2018, 5:33am UTC](https://forum.kirupa.com/t/breaking-into-letters-with-the-spread-operator/639100/1 "2018-12-10T05:33:40Z")

</div>

I was messing around with the spread operator, and I wanted to share this little trick:

```
var word = "dinosaurs";

// old-school
var letters = word.split('');
console.log(letters);

// new-school with the spread operator
var letters2 = [...word];
console.log(letters2);

```

You can use the spread operator across a variety of similar scenarios like with arrays or object properties…sort of 😉

Cheers,  
Kirupa

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [December 10, 2018, 1:39pm UTC](https://forum.kirupa.com/t/breaking-into-letters-with-the-spread-operator/639100/2 "2018-12-10T13:39:11Z")

</div>

> [@kirupa](#):
>
> …sort of

Object properties spread just fine 🕶

```auto
var word = {
  start: 'dino',
  end: 'saurs',
};

console.log({ ...word }); // {start: 'dino', end: 'saurs'}

```

It’s a different kind of spread, an _object spread_ vs. an _array spread_, but array spreading only works on iterables - values that implement `[Symbol.iterator]`. String is such an object, in fact the only primitive that does so. Object spread works on any value spreading own, enumerable properties into the new literal - and, in the case of strings, given that it is also _array like_ beyond implementing `[Symbol.iterator]` (indices referring to characters, e.g. `'dinosaurs'[0] === 'd'`), its characters:

```auto
var word = 'dinosaurs';
console.log({ ...word });
// {0: "d", 1: "i", 2: "n", 3: "o", 4: "s", 5: "a", 6: "u", 7: "r", 8: "s"}

```

Other objects can also be blessed with array spreading capabilities if they also implement a `[Symbol.iterator]`. An easy way to do this for generic use cases is using `Object.values` along with `Array.prototype.values` (confusingly enough, Object static variations [`values`, `keys`, `entries`] return arrays while Array instance methods return array iterators, which is what is needed for `[Symbol.iterator]`).

```auto
var word = {
  start: 'dino',
  end: 'saurs',
  [Symbol.iterator]: () => Object.values(word).values(),
};

console.log([...word]); // ['dino', 'saurs']

```

Note that for function argument spreading, array spread is used because arguments are an unnamed, ordered list of values.
