What is { XYZ } = Object called ? where Object have xyz as property.
3 Answers
It's called the destructuring assignment, and it's used to extract object properties or array elements. Here's an example:
const object = { xyz: "abc" };
let { xyz } = object;
console.log(xyz);
The above defines an object with a property called xyz
. It then saves the value of that property to a variable named xyz
. It's essentially shorthand for doing this in ES5 (because destructuring was introduced in ES6):
var object = { xyz: "abc" };
var xyz = object.xyz;
console.log(xyz);
You can also rename the destructured variable:
const object = { xyz: "abc" };
const { xyz: letters } = object;
console.log(letters);
Just as you would the variable:
var object = { xyz: "abc" };
var letters = object.xyz;
console.log(letters);
It also works with functions:
const logName = ({ name }) => console.log(name);
const john = { age: 42, name: "Mr. Doe" };
logName(john);
Which is the ES6 equivalent of:
function logName(person) {
var name = person.name;
console.log(name);
}
var john = { age: 42, name: "Mr. Doe" };
logName(john);
![](../../users/profiles/10221765.webp)
- 38,499
- 10
- 36
- 67
The term you are looking for is 'destructuring assignment'.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
![](../../users/profiles/348975.webp)
- 10,259
- 1
- 28
- 55
It's called Destructuring assignment https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
![](../../users/profiles/2340824.webp)
- 1,979
- 1
- 13
- 18