You can achieve this in quite a few different ways.
let foo = { bar: 'Hello World' }; foo.bar; foo['bar']; The bracket notation is specially powerful as it let's you access a property based on a variable:
let foo = { bar: 'Hello World' }; let prop = 'bar'; foo[prop]; This can be extended to looping over every property of an object. This can be seem redundant due to newer JavaScript constructs such as for ... of ..., but helps illustrate a use case:
let foo = { bar: 'Hello World', baz: 'How are you doing?', last: 'Quite alright' }; for (let prop in foo.getOwnPropertyNames()) { console.log(foo[prop]); } Both dot and bracket notation also work as expected for nested objects:
let foo = { bar: { baz: 'Hello World' } }; foo.bar.baz; foo['bar']['baz']; foo.bar['baz']; foo['bar'].baz; Object deconstructiondestructuring
We could also consider object deconstructiondestructuring as a means to access a property in an object, but as follows:
let foo = { bar: 'Hello World', baz: 'How are you doing?', last: 'Quite alright' }; let prop = 'last'; let { bar, baz, [prop]: customName } = foo; // bar = 'Hello World' // baz = 'How are you doing?' // customName = 'Quite alright'