Can anyone explain how the why/how the below method of assigning keys in JavaScript works?
a = "b" c = {[a]: "d"}
return:
Object {b: "d"}
It's the new ES2015 (the EcmaScript spec formally known as ES6) computed property name syntax. It's a shorthand for the someObject[someKey] assignment that you know from ES3/5:
someObject[someKey]
var a = "b" var c = {[a]: "d"}
is syntactic sugar for:
var a = "b" var c = {} c[a] = "d"
1.4m articles
1.4m replys
5 comments
57.0k users