I've seen this in a repository, but I'm not quite sure what is exactly happening. Is the var value replaced or is the child question of value taken from var data?
const { value: question } = data;
const { value } = data;
I've seen this in a repository, but I'm not quite sure what is exactly happening. Is the var value replaced or is the child question of value taken from var data?
const { value: question } = data;
const { value } = data;
const { value } = data;
creates a block-scoped constant named value
, and assigns to it data.value
.
It's identical to
const value = data.value;
const { value: question } = data;
creates a block-scoped constant named question
, and assigns to it data.value
.
It's identical to
const question = data.value;
A property can be unpacked from an object and assigned to a variable with a different name than the object property.
var o = {p: 42, q: true};
var {p: foo, q: bar} = o;
console.log(foo); // 42
console.log(bar); // true
Here, for example, var {p: foo} = o takes from the object o the property named p and assigns it to a local variable named foo.