Destructuring assignment
Destructuring assignment
The Destructuring assignment is JavaScript syntax that allows values to be taken from a compatible datatype without having to use dot notation or indexes.
To use the destructuring assignment an object or array can be opened up, and given names to the values in place to extract them from the datasource.
For the following example: const [itemA, itemB] = [0, 1];
, itemA
and itemB
are now constants that refer to the values 0
and 1
in the array.
Here is another example using an object:
const data = {
coffee: 'robusta',
strength: 5,
};
const { coffee, strength } = data;