Day 61: React Course on Udemy

There was an interesting tidbit I learned today regarding JavaScript object syntax. You can access values of objects with both dot and bracket notation.

let car = {
  make: 'Honda',
  color: 'red'
}

console.log(car.make) // Honda
console.log(car['make']) //Honda

Why would you want to use bracket notation though when it's so much simpler to use dot notation? Well with dot notation, we can't refer to properties that start with numbers, have spaces, or use reserved key words (function, special characters, const etc.)

let car = {
  'origin year': 1999,
  color: 'red'
}

console.log(car.origin year)     // SyntaxError
console.log(car['origin year']) // 1999