JavaScript Tips: Different ways of getting Object Properties

Sam Ngu
Feb 1, 2022

There are 2 ways to get properties from an object.

  1. use .
  2. use []

eg.

const car = {
colour: 'red',
brand: 'tesla'
}

To get the colour property, we can either:

console.log(car.colour); // red

or:

console.log(car["colour"]); // red

Why do we use the square bracket syntax?

Using the square bracket syntax allows us to get properties dynamically, i.e. we can put another variable inside the square bracket.

For example:

const colourText = "colour";
console.log(car[colourText]); // red

--

--