Home > Software design >  Javascript chaining asign variable
Javascript chaining asign variable

Time:09-27

Can someone help me ?

var { id } = [response] = [{ id: 1 }];

console.log(id)

Output : undefined, Expected output : 1

CodePudding user response:

The result of an assignment expression is always the value that was assigned (i.e. an array in your case).

var [{id}] = [response] = [{id : 1}]

console.log(id)

Note that this will create a global response variable if the variable isn't declared yet (or throws an error in strict mode).

It might be easier to understand if you performed this in two steps:

var [response] = [{id: 1}];
var {id} = response;
  • Related