1

Can someone explain what's going on here?

let b; 
({a:b}={a:'1234'});
console.log("b=", b);

when i run above code, i get output b=1234. I'm just trying to understand what's happening here. I'm new to typescript coming from Java background!

Terry
  • 63,248
  • 15
  • 96
  • 118
rpcredit
  • 31
  • 1
  • 4
  • 2
    It’s object destructuring + assignment to a new variable. This is an ES6 syntax and has nothing to do with TypeScript. – Terry Feb 04 '22 at 22:37
  • 1
    See the docs: [Object destructuring: Assignment separate from declaration](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#object_destructuring) (combined with *'Assigning to new variable names'*) – pilchard Feb 04 '22 at 22:42
  • 1
    it answers my question, not something easy to get it naturally. i'll dig through docs around the ES6 object destructuring concept! – rpcredit Feb 04 '22 at 22:43
  • 1
    this might be easier to reason about if you write it as `let {a: b} = {a: '1234'}` – tromgy Feb 04 '22 at 22:45

0 Answers0