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}');

Required Knowledge

Related Katas


Difficulty Level


First Published

27 March 2015


3 tests to solve