JS: Property Key

By Xah Lee. Date: . Last updated: .

Property Key

Property String Key Example

/* string property keys */
const jj = { "a": 1, b: 2 };

const kk = Object.keys(jj);

console.log(typeof kk[0] === "string");
console.log(typeof kk[1] === "string");

πŸ›‘ WARNING: Implicitly Convention of Number to String

If a key is not symbol or string, it is converted to string implicitly.

// int keys are auto converted to string type
console.log(typeof Object.keys({ 3: "b" })[0] === "string");

Get Variable Value as Key Name (Computed Property Key)

To evaluate a variable as property key, put it inside a square bracket, like this:

obj = {[expr]:value}

Property Symbol Key

JavaScript, Property