0

I am trying to reuse the variables first and second, I am able to update the variable directly, but I can't reuse them for another destructuring statement.

let [first, second] = [1, 2]
[first, second] = [3, 4]

Variables are declared using let, so we can reassign. But, I am getting a runtime error

Youssef AbouEgla
  • 1,445
  • 10
  • 21
Aman Kumar
  • 23
  • 3
  • 3
    You are missing `;` after the first line. – VLAZ Jun 13 '20 at 19:19
  • 1
    Also relevant: [Do we need semicolon at the end?](https://stackoverflow.com/q/4002234) [Do you recommend using semicolons after every statement in JavaScript?](https://stackoverflow.com/q/444080) [Javascript “Uncaught TypeError: object is not a function” associativity question](https://stackoverflow.com/q/4026891) [Javascript array destructuring assignment gives strange errors](https://stackoverflow.com/q/57006526) – VLAZ Jun 13 '20 at 19:23
  • @VLAZ The problem is that my project uses prettier for formatting. And, I don't get to edit the .prettierrc. So, even if I put a ; there, it would be taken off when the format script runs prior to a commit. – Aman Kumar Jun 13 '20 at 19:31
  • Then write `; [first, second] = [3, 4]` – VLAZ Jun 13 '20 at 19:31
  • @VLAZ The explanation is a bit difficult to follow. But, thanks for sharing the resource. I really appreciate it. – Aman Kumar Jun 13 '20 at 19:31
  • 2
    As a side note, it's a REALLY BAD IDEA to have enforced style that leads to bugs, as you can see here. I highly suggest editing the configuration to avoid future problems. – VLAZ Jun 13 '20 at 19:33
  • @VLAZ Prettier is automatically adding this "([first, second] = [3, 4])", but it still throws an error. However, the "; [first, second] = [3, 4]" solution worked. Thanks so much – Aman Kumar Jun 13 '20 at 19:34
  • I've edited out the `()` one. My mistake - it will not work. – VLAZ Jun 13 '20 at 19:35
  • @VLAZ Got you. Editing the config is a nice idea, will do that – Aman Kumar Jun 13 '20 at 19:37
  • You may find this article useful: [Your Guide to Semicolons in JavaScript](https://news.codecademy.com/your-guide-to-semicolons-in-javascript/). It's in simpler language. – Mubaraq Wahab Jun 13 '20 at 19:42

1 Answers1

-3

first, you must declare a new type object has two property first, second

var varaibale = {first:1, second:2};
varaibale.first = 3;
varaibale.second = 4;
console.log(varaibale);
AhmedSeef
  • 13
  • 2