Object destructuring

Object destructuring

You can also destructure objects:

1
2
3
4
5
6
let o = {
  a: "foo",
  b: 12,
  c: "bar"
}
let {a, b} = o;

This creates new variables a and b from o.a and o.b. Notice that you can skip c if you don’t need it.

Like array destructuring, you can have assignment without declaration:

1
({a, b} = {a: "baz", b: 101});

Notice that we had to surround this statement with parentheses. JavaScript normally parses a { as the start of block.

doc_TypeScript
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.