Dot notation vs Bracket notation
Both notations can access object properties. But the question is often which one should I use 🤔. Wonder no more, just follow Airbnb’s style guide. Always use Dot. And when you want to access object property with a variable, use Bracket 👍
// Dot notation vs Bracket notationconst variable = 'cookie';const snack = {
cookie: '🍪'
};// ✅ Dot: access property
snack.cookie;// ✅ Bracket: access property with variable
snack[variable];
Accessing Object Properties
There are 2 ways to access object properties. Dot and Bracket.
const obj = {
name: 'value'
};// Dot Notation
obj.name; // 'value'// Bracket Notation
obj['name']; // 'value'
Dot Notation for the win
I remember when I was first learning this. Understanding the 2 different ways was simple. Nothing too complicated. But my concern was never about the different notations. My biggest dilemma was, WHICH SHOULD I USE?? 🤯
If you were like me! Here’s the breakdown. They both do the same. So here is the simple rule. By default, just use the Dot notation.