Categories
JavaScript Answers

How to get value in an object’s key using a variable referencing that key with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *