-4

I am getting an esLint warning on the below to use object destructuring.

Can I do this reassigning an already declared variable as per the below example, or should I just ignore the esLint warning.

let testA = 0;

if (condition) {
    testA = myObj.testA;
    // cannot do 'const { testA } = myObj;' because I have already declared testA in the above scope
}
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
user3284707
  • 3,033
  • 3
  • 35
  • 69
  • Can't do it with `const` and use same variable name ... no – charlietfl Feb 23 '19 at 19:13
  • `let testA = {myObj}; if (!condition) testA = 0;` :-P (Joking.) Yes, you can ignore eslint here, using destructuring is possible but not much of an improvement. – Bergi Feb 23 '19 at 19:17
  • Thanks for the reply. I was thinking I could do what is suggested below, but wasn't sure how/why that would be an improvement, other than to get rid of the warning. – user3284707 Feb 23 '19 at 19:21

1 Answers1

-3

So const is unchangeable declaration. You can't assign a variable which is use before.

Replace

const { testA } = myObj;

with

const { testA: testATemp } = myObj; //this is assignment while destructing
console.log(testATemp);
Subhendu Kundu
  • 3,618
  • 6
  • 26
  • 57