Javascript: How to dynamically access object property using variable

There are two ways to access properties of an object:

1. Dot notation

const person1 = {
  firstname: 'John',
  lastname: 'Doe'
};
console.log(person1.firstname) //John 

2. Bracket notation

The value between the brackets can be any expression. Therefore, if the property name is stored in a variable, you have to use bracket notation:

const person1 = {
  firstname: 'John',
  lastname: 'Doe'
};
console.log(person1['firstname']) //John