jskatas.org Continuously Learn JavaScript. Your Way.

Destructuring: defaults

When destructuring you can also use default values.

When destructuring you can also provide default values

just assign a default value, like so a=1
const [a:1] = []; assert.equal(a, 1);
for a missing value
const [b=2] = [1,,3]; assert.equal(b, 2);
in an object
const [a, b=2] = {a: 1}; assert.equal(b, 2);
if the value is undefined
const {a, b} = {a: 1, b: void 0}; assert.strictEqual(b, 2);
also a string works with defaults
const [b=2] = '1'; assert.equal(a, '1'); assert.equal(b, 2);

Required Knowledge

Related Katas


Difficulty Level


First Published

31 March 2015


5 tests to solve