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.