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??
const xline is unused). The syntaxob["foo"]is the same asob.foo, but you can only use the latter syntax because "foo" is a valid identifier. You could useob["foo-bar"]but notob.foo-bar. The bracket syntax also allows you to use a property where you don't know the key at compile time, egconst key = calculateKey();ob[key]