There are two primary ways to access properties in JavaScript objects:
- Dot notation: direct access by key
- Bracket notation: indirect access by value
These two types of access can be quite confusing at the beginning, but with a solid understanding of how variables & values work the mystery fades away. Knowing when to use one or the other can take some time and experience, but learning how they work is not so tricky with the right visualizations.
var og = {first: 'fifty', last: 'cent'};
var curtis = 'first';
var jackson = 'last';
console.log(og.first); // log: fifty
console.log(og.last); // log: cent
console.log(og[curtis]); // log: fifty
console.log(og[jackson]); // log: cent