Categories
JavaScript Answers

How to use variable keys to access values in JavaScript objects?

Spread the love

Sometimes, we want to use variable keys to access values in JavaScript objects.

In this article, we’ll look at how to use variable keys to access values in JavaScript objects.

How to use variable keys to access values in JavaScript objects?

To use variable keys to access values in JavaScript objects, we can use square brackets.

For instance, we write

const x = { test: "hi" };
alert(x.test);
alert(x["test"]);

to put "test" in the square brackets to return the value of the test property in x.

Therefore, x["test"] is 'hi'.

Conclusion

To use variable keys to access values in JavaScript objects, we can use square brackets.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.