jskatas.org Continuously Learn JavaScript. Your Way.

Destructuring: string

Destructuring can also be done on strings.

Destructuring also works on strings

destructure every character, just as if the string was an array
let a, b, c = 'abc'; assert.deepEqual([a, b, c], ['a', 'b', 'c']);
missing characters are undefined
const [a, c] = 'ab'; assert.equal(c, void 0);
unicode character work too
const [space, coffee] = 'a ☕'; assert.equal(coffee, '\u{2615}');