Assign Object key with a variable

ES5

var newValue = "This is value.";
var newKey = "originalKey";
var obj = {newKey: newValue};

Using obj[key] = value pattern.

var newKey = "This is key."
var obj = {};
obj[myKey] = myValue;

ES6

Assign variable with brackets when defining the object.

let newValue = "This is value.";
let newKey = "originalKey";
let obj = {[newKey]: newValue};

関連記事