๐ Mastering JavaScript Object Properties: Dot Notation vs. Bracket Notation

Alright โ imagine you just joined a game where players can build their own digital identity cards. Every player can create labels like:
- Name
- Level
- Favorite food
- Even crazy ones like โBest Snack ๐ฟโ
Behind the game engine, JavaScript is using something called objects to store all that information.
Today, youโre learning how JavaScript reads those labels โ and why sometimes you must use a different method to access them.
๐งฑ JavaScript Objects โ Your Digital Locker
Think of an object like a locker with labeled sections.
const player = {
name: "Ken",
level: 10
};
๐ง What Each Part Means
| Code | Meaning |
|---|---|
const |
Keyword to create a constant variable (cannot be reassigned) |
player |
Variable name storing the object |
{ } |
Creates an object |
name, level |
Keys (labels) |
"Ken", 10 |
Values (stored data) |
๐ช Method 1 โ Dot Notation (The Front Door)
This is the easiest way to access object data.
player.name = "Ken";
๐ง How It Reads
playerโ go to the object.โ enter insidenameโ select that property
โ Dot Notation Works When Keys Are โCleanโ
player.age = 20;
player.user_1 = "Online";
player.$coins = 500;
โ Dot Notation Fails When Keys Are Weird
If keys contain:
- Spaces
- Emojis
- Special symbols
Dot notation breaks.
player.favorite food = "Rice"; // โ ERROR
Because JavaScript thinks:
favoriteis propertyfoodis unexpected extra word
๐งฐ Method 2 โ Bracket Notation (The Master Key)
Bracket notation can use any string as a key.
player["favorite food"] = "Rice";
๐ง Why It Works
Because inside brackets, JavaScript treats everything as a string key.
โ Works With Anything
player["Best Snack ๐ฟ"] = "Popcorn";
player["home address"] = "Lagos";
player["score-per-level"] = 50;
๐ข Numbers as Keys (JavaScript Secret)
You can do this:
const obj = {};
obj[123] = "Hello";
But JavaScript secretly converts number keys into strings.
So these are the same:
obj[123];
obj["123"];
๐งช Expressions Inside Brackets (Magic Feature)
JavaScript can calculate keys first.
const obj = {};
obj["12" + "3"] = "Hi";
obj[120 + 3] = "Hello";
๐ง Step-by-Step
First Line
"12" + "3"
Both are strings โ JavaScript joins them โ "123"
So becomes:
obj["123"] = "Hi";
Second Line
120 + 3
Math happens โ 123
Then JavaScript converts to string โ "123"
So becomes:
obj["123"] = "Hello";
๐คฏ Final Result
Second value overwrites first.
{
"123": "Hello"
}
๐ฎ Real Life Example โ Game Inventory
const inventory = {};
inventory["Magic Sword โ"] = 1;
inventory["Health Potion ๐งช"] = 5;
let newItem = "Dragon Egg ๐";
inventory[newItem] = 2;
console.log(inventory);
๐ง Syntax Explained Simply
const
Creates a variable that shouldnโt be reassigned.
{ }
Creates an object container.
[ ]
Used to access keys using strings or calculations.
=
Stores a value.
console.log()
Prints output to screen.
๐ Dot vs Bracket โ Simple Rule
| Situation | Use |
|---|---|
| Simple property names | Dot |
| Spaces / Emojis | Bracket |
| Dynamic keys (variables) | Bracket |
| Calculated keys | Bracket |
๐ฏ Golden Rule Story
If your key is clean and simple, use dot notation. If your key is crazy, user-made, or calculated, use bracket notation.
๐ Review Questions
1๏ธโฃ Why will this cause an error?
player.favorite food = "Rice";
2๏ธโฃ What will this output and why?
const obj = {};
obj[456] = "Hello";
console.log(obj["456"]);
3๏ธโฃ What is the final object?
const obj = {};
obj["10" + "0"] = "A";
obj[50 + 50] = "B";
4๏ธโฃ Rewrite this correctly using bracket notation:
const user = {};
user.best color = "Blue";
5๏ธโฃ In a website where users create custom profile fields like:
"Dream Car ๐""Best Food ๐ฒ"
Which notation should you use and why?
6๏ธโฃ What will this output and why?
const obj = {};
let key = "user";
obj[key + "Name"] = "Ken";
console.log(obj["userName"]);
๐ Explain step by step how the key was formed.
7๏ธโฃ Predict the output:
const data = {};
data[200] = "First";
data["200"] = "Second";
console.log(data[200]);
๐ Which value will print and why?
8๏ธโฃ Find and Fix the Error:
const game = {};
game.player score = 50;
๐ Rewrite it so it works correctly.
9๏ธโฃ What will be inside the object?
const obj = {};
obj["5" * 2] = "Result";
console.log(obj);
๐ Hint: "5" * 2 is NOT string joining.
๐ Real Thinking Question
You are building a form where users can create custom fields like:
"My Pet Name ๐""Favorite Quote โค๏ธ""Best Teacher Ever โญ"
Users type these names themselves.
๐ Which notation should you use? ๐ Why would dot notation be risky here?