Sometimes, we want to get value in an object’s key using a variable referencing that key with JavaScript.
In this article, we’ll look at how to get value in an object’s key using a variable referencing that key with JavaScript.
How to get value in an object’s key using a variable referencing that key with JavaScript?
To get value in an object’s key using a variable referencing that key with JavaScript, we can use square brackets.
For instance, we write
console.log(obj[name]);
to get the name property of the obj object with obj[name].
name is a string with the key name of the property in obj we want to get.
Conclusion
To get value in an object’s key using a variable referencing that key with JavaScript, we can use square brackets.