⊗jsPmObVK 90 of 502 menu

Object keys in variables in JavaScript

Object keys can be stored in variables. Let's look at an example. Let's have the following object:

let obj = {a: 1, b: 2, c: 3};

Let the variable store the key:

let key = 'a';

Derive an object element by key:

console.log(obj[key]); // shows 1

Given the following object:

let obj = {x: 1, y: 2, z: 3};

The variable key is given, which stores one of the keys of our object. Use this variable to output the corresponding element of the object.

enru