Accessing Object Properties
Once you have an object, you need to access its properties. There are two ways to do this!
Method 1: Dot Notation (Most Common)
⚡ javascript
1
2
3
4
5
6
7
8
9
const phone = {
brand: 'iPhone',
model: '15 Pro',
storage: 256
}
console.log(phone.brand) // 'iPhone'
console.log(phone.model) // '15 Pro'
console.log(phone.storage) // 256Method 2: Bracket Notation
⚡ javascript
1
2
3
4
5
6
console.log(phone['brand']) // 'iPhone'
console.log(phone['model']) // '15 Pro'
// Useful when property name is in a variable
const prop = 'storage'
console.log(phone[prop]) // 256When to use each:
- Dot notation: When you know the property name
- Bracket notation: When using variables or special characters