What is this javascript syntax where you have () around the whole variable expression?

This is Destructuring Assignment without declaration. Here customer variable is already declared above and a value is being assigned with response.body.customer

From the documentation:

The parentheses ( ... ) around the assignment statement are required when using object literal destructuring assignment without a declaration.

{a, b} = {a: 1, b: 2} is not valid stand-alone syntax, as the {a, b} on the left-hand side is considered a block and not an object literal.

However, ({a, b} = {a: 1, b: 2}) is valid, as is var {a, b} = {a: 1, b: 2}

Your ( ... ) expression needs to be preceded by a semicolon or it may be used to execute a function on the previous line.


It forces expression context so that the first { is not treated as the start of a block.

Tags:

Javascript