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 conversion 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