⭐destructuring assignment
JS ⟩ operator ⟩ assignment ⟩ destructuring
in a destructuring assignment,
left-hand side: specifies one or more variable names using a syntax that mimics array / object literal syntax.
// destructuring assignment
let {a} = {a: 1, b: 2};
let [a, b=0, , ...rest] = iterable;destructuring assignment can be used to provide initial value(s).
often used in tagged template.
can be used with iterables.
// ⭐ array destructuring
let [
a,
b = 0, // default value
, // ignore element
...rest // rest elements as an "array"
] = array
// ⭐ object destructuring
let {
a,
b = 0, // default value
c: newPropName, // new variable name
...rest // rest properties as an "object"
} = object
// ⭐ smart function parameters
function f(a, b, {
c,
d: newPropName = 0, // new parameter name (with default value)
...rest
} = {}){
// do something with: a, b, c, newPropName, rest
}Last updated
Was this helpful?