1
let b = { a: 123123123 };
try {
  ({ a } = b); <<< what is the meaning of () 

} catch (error) {}
console.log({'a':a})

why this script work, what is the meaning of () in try-catch .

the { a } = b is wrong but ({ a } = b) is correct and the variable will go out of try-catch and give new value to a.

if there are a function in () may be a IIFE, if in expressions the () is a Grouping operator.and in my example,what is the meaning of ()

radiorz
  • 1,459
  • 4
  • 18
  • 36
  • 2
    [Destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment). `()` are needed here to prevent `{}` from being parsed as the parenthesis of a block. You wouldn't need `()` if `a` wasn't available in the scope. If that was the case, you would write this statement as: `const { a } = b;` – Yousaf Sep 16 '21 at 09:07
  • 1
    A `{` at the start of the line is interpreted as a code block, so using grouping `()` ensures it's treated as destructuring. – VLAZ Sep 16 '21 at 09:08
  • Is there another `a` declared in a higher scope? – zero298 Sep 16 '21 at 09:08
  • 1
    @zero298 it's not necessary, because the block is treated as an expression, hence `a` is implitly assigned, although I think `a` in this case is an implicit global. – briosheje Sep 16 '21 at 09:11

1 Answers1

4

A pair of parenthesis force the contents to be treated as an expression instead of a statement.

This causes the { to be seen at the start of an object or dereferencing structure (the latter in this case) instead of a block.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335