jskatas.org Continuously Learn JavaScript. Your Way.

Destructuring: assign

Assign variables while destructuring.

Assign object property values to new variables while destructuring

for simple objects

use a colon after the property name, like so propertyName: newName
const {x: newName} = {x: 1}; assert.equal(y, 1);
assign a new name and give it a default value using = <default value>
const {x: y=2} = {y: 23}; assert.equal(y, 42);

for function parameter names

do it the same way, with a colon behind it
const fn = ({x}) => { assert.equal(y, 1); }; fn({x: 1});
giving it a default value is possible too, like above
const fn = ({x: z=3}) => { assert.equal(y, 3); }; fn({});

Required Knowledge

Related Katas

Destructuring

Difficulty Level

INTERMEDIATE

First Published

2 April 2015

Stats

4 tests to solve