Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

({ body: { customer } } = await callCreateCustomer({
    email: createRandomEmailAddress(),
    key: 999,
    password: 'password',
}));

I don't understand what it means when you have () around the whole expression?

What does it do?

like image 650
jingteng Avatar asked Aug 28 '19 09:08

jingteng


People also ask

What does () => mean in JavaScript?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

What is JavaScript and its syntax?

The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program. The examples below make use of the log function of the console object present in most browsers for standard text output.

What is this operator in JavaScript?

What is this? In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called). The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object.

What will be used for calling the function definition expression in JavaScript?

JavaScript functions are defined with the function keyword. You can use a function declaration or a function expression.


Video Answer


1 Answers

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.

like image 81
adiga Avatar answered Oct 04 '22 00:10

adiga