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.

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 *