The type of assignment I want to learn about probably has lots of explanations online, but I can't find them because I don't know what it's called. So what do you call this form of variable assignment in Javascript const {a, b, c} = x;
?
Asked
Active
Viewed 462 times
4

achalk
- 3,229
- 3
- 17
- 37
-
1Object destructuring assingment – guest271314 Sep 19 '17 at 18:13
-
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment – slawek Sep 19 '17 at 18:14
-
it means that `x` is an object containing properties `a`, `b` and `c`... – assembler Sep 19 '17 at 18:14
-
@guest271314 I'd argue this isn't a duplicate, because I'm not asking what the code does, I'm just asking for the name of the technique. Also, it would be really hard for people to find the existing question through search, as the syntax it's asking about doesn't feature in the title. – achalk Sep 19 '17 at 18:18
-
@adc17 Do the answers at linked Question not properly describe the name of the syntax? – guest271314 Sep 19 '17 at 18:21
-
@guest271314 Fair enough, I guess so. I just figured that the question I asked is different. Also, for people who don't know the name of this type of code and are trying to figure out what it does, I think my question is helpful. They're not going to find 'I don't understand what's going on this syntax javascript' if they do a search with an example of the code, but they may find this question. – achalk Sep 19 '17 at 20:01
-
@adc17 Do you mean if an individual searches for the exact code that you included in the title of the Question, or "variable assignment"? Do you suggest that the Question be re-opened? – guest271314 Sep 19 '17 at 20:03
-
@guest271314 you're right, a search for `const {x, y, z} = n;` doesn't find it, so I guess that aspect of its value is pretty limited. – achalk Sep 19 '17 at 20:04
1 Answers
3
It is called destructuring assignment.
It takes an object as value and variables, where the names reflects the same named properties and assigns the value to it.
const x = { a: 1, b: 2, c: 3, d: 4 };
const { a, b, c } = x;
console.log(a, b, c);

Nina Scholz
- 376,160
- 25
- 347
- 392