2 ms·
It’s not that, it’s a dynamic key in an object literal. const key = "foo"; const obj = { [key]: "bar" }; console.log(obj.foo); // prints "bar"
by cluckindan 1y ago
It’s not that, it’s a dynamic key in an object literal.
const key = "foo";
const obj = { [key]: "bar" };
console.log(obj.foo); // prints "bar"
- MrJohz 1y agoThat's also possible, and it's common when using this pattern, but the specific syntax in the original question was I believe property access, and not part of a property literal. I didn't bring that up because I thought my comment was long enough and I wanted to explain that specific syntax. But yeah, you also have this syntax to set properties in object literals, and a similar syntax in classes.