Last Updated: February 25, 2016
·
239
· ivanovb

In Javascript a Key is a Symbol

If you want to build at runtime an object with variable keys and values, remember that in Javascript, a key is actually a primitive symbol-value that doesn't get evaluated.

var f1 = function(key, value) {
  return {key: value};
}

var f2 = function(key, value) {
  var _o = {key: value};
  return _o;
}

var f3 = function(key, value) {
  var _o = {}; 
  _o[key] = value;
  return _o;
}

f1('hi', 'hello'); // -> {key: 'hello'};
f2('hi', 'hello'); // -> {key: 'hello'};
f3('hi', 'hello'); // -> {'hi': 'hello'};