Syntax: const {} = variableName, can someone explain or point me in the right direction

What does this syntax mean in JavaScript (probably ES6):

const {} = variablename;

I'm currently trying to gain control over React. In many examples, I came across this syntax. For instance:

const {girls, guys, women, men} = state; 
+5
source share
1 answer

First of all, this has nothing to do with Reaction. This is part of ECMAScript 6 (or JavaScript 2015, if you prefer).

What you see here is called Destruction Destination :

 const {girls, guys, women, men} = state; // Is the same as const girls = state.girls; const guys = state.guys; const women = state.women; const men = state.men; 



You will probably come across a similar patter when learning React:

 import { methodA, methodB } from "my-module"; 

In this case, you have a module called my-module , which displays some functions. With the syntax import {} from you choose which functions you want to import. Note that this is not a destructive assignment, although it works the same way.

+6
source

Source: https://habr.com/ru/post/1243074/


All Articles