0
let a

function f() {
  [a] = [1] // <<-----
  return a
}
console.log(f())

How does [a] = .... is called? it's not a normal assignment because a is defined elsewhere.

Bonus:

Why prettier thinks that there should be semicolon before the [?

Stav Alfi
  • 13,139
  • 23
  • 99
  • 171
  • 1
    I'm pretty sure it's called an "error". Also `let a: number` is not JavaScript either. *edit* nope it's simple destructuring, sorry. Weird though. – Pointy Dec 08 '20 at 16:39
  • 3
    If there's only one item being assigned, it's a really strange way of assigning the one item in the array on the right to the first item in the destructuring syntax on the left, the `a`. – CertainPerformance Dec 08 '20 at 16:40
  • 2
    "*Why prettier thinks that there should be semicolon before the `[`?*" - because that's the rule when you omit semicolons elsewhere. If you don't know the rules, you should always use semicolons. – Bergi Dec 08 '20 at 16:41
  • @Pointy it's typescript. edited. thanks. – Stav Alfi Dec 08 '20 at 16:43
  • That is array destructuring, but probably because not in strict mode, there's no var, let or const. The correct way to do it would be: const [ a ] = [ 1 ] ; – Oscar Calderon Dec 08 '20 at 16:43
  • @CertainPerformance it's just a small example. don't take it too serious. – Stav Alfi Dec 08 '20 at 16:43
  • "*it's not a normal assignment because a is defined elsewhere*" doesn't make it "not an assignment". If you have `let x;` in one place and `x = 42` in another, it's still an assignment. – VLAZ Dec 08 '20 at 16:43
  • Do you have code before the `[a] = [1]` line? Omitting semicolon can cause errors. `[a]` will act as a bracket notation along with the expression in the previous line [ES6 Array destructuring weirdness](https://stackoverflow.com/q/38050219) – adiga Dec 08 '20 at 16:44
  • @OscarCalderon you don't have to destructure at declaration time. strict/loose mode is irrelevant here. – VLAZ Dec 08 '20 at 16:44
  • @adiga I made this example like this to show that there is no code before it in the function so semicolon is not needed – Stav Alfi Dec 08 '20 at 16:45
  • See: [Destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) – 3limin4t0r Dec 08 '20 at 16:49
  • A semicolon *would* be needed in many cases, and the linter is encouraging you to be stylistically consistent as well. – CertainPerformance Dec 08 '20 at 16:50
  • That is an eslint issue then. You can turn it off if possible or use `;[a] = [1];`. Looks weird though – adiga Dec 08 '20 at 16:50
  • @adiga Thanks! I think it's prettier issue because prettier is the one that complains and I think they didn't cover this special case when it appears before `{`. this was the answer I was looking for. Thank you very much and everyone else who helped! :) – Stav Alfi Dec 08 '20 at 16:55
  • @adiga It would be `;[a] = [1]`, not `;[a] = [1];`, prettier only suggests a semicolon at the beginning of a line when the [semicolons](https://prettier.io/docs/en/options.html#semicolons) `false` option is set. If semicolons is set to `true` there is no need to have a the line begin with a semicolon (since the previous line should end with it) and `[a] = [1];` would be the suggested formatting. – 3limin4t0r Dec 08 '20 at 17:07
  • @VLAZ I don't get your comment. What I mean is, this is destructuring syntax, but the missing const/let/var word indicates that it is not in strict mode, otherwise the interpreter would complain about it for not being properly declared. About declaration time, it is possible to destructure at declaration time. – Oscar Calderon Dec 09 '20 at 17:08
  • @OscarCalderon [no, strict mode does not change this code](https://jsbin.com/miqajokopa/edit?js,console). – VLAZ Dec 09 '20 at 17:26

0 Answers0