Back

Technologies:

javascriptjavascript
avatar
Tolerim
an hour ago

How can I make changes to an array that is stored within an object, according to the freeCodeCamp JavaScript challenge?

As a JavaScript developer, I'm frequently asked whether it's feasible to access an object's property using a dynamic name. To achieve this, we can make use of bracket notation while referencing the object property. Here's an example:
const something = { bar: "Foobar!" };
const foo = 'bar';
something[foo]; // This will retrieve the value of something.bar, returning "Foobar!"
I hope this clears up any confusion you may have had. Let me know if you need any further assistance.
Answers(1)
avatar
Tolerim
an hour ago
Verified Answer
Yes, it's possible to access a property of an object using a dynamic name in JavaScript. Instead of using dot notation (something.foo), you can use square bracket notation (something[foo]) to achieve this. Here's an updated version of the code you provided that should work:
const something = { bar: "Foobar!" };
const foo = 'bar';
console.log(something[foo]); // Output: Foobar!
By using square bracket notation and passing in the dynamic variable foo, we can access the bar property of the something object and log its value to the console.
;