ES6 allows you to use an expression in brackets []
. It’ll then use the result of the expression as the property name of an object. For example:
let propName = 'c';
const rank = {
a: 1,
b: 2,
[propName]: 3,
};
console.log(rank.c); // 3
In this example, the [propName]
is a computed property of the rank
object. The property name is derived from the value of the propName
variable