Reputation: 1619
code:
const x = 6;
const ob = {x: [6.1, 6.5]}
console.log(ob) // {x: [6.1, 6.5]}
const y = 6;
const ob = {[y] : [6.1, 6.5]};
console.log(ob) // {6: [6.1, 6.5]}
Why square brackets allow use the value of the variable as object key, is this related to destructuring??
Upvotes: 6
Views: 1459
Reputation: 249
The square brackets allow for computed property keys. In other words, the value of the variable inside of the square brackets is processed before the value is accessed.
Upvotes: 4