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 inside
  • name โ†’ 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:

  • favorite is property
  • food is 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?



<
Previous Post
๐Ÿง  Debugging Made Simple with console.table
>
Next Post
JavaScript Objects Simplified: A Summary of MDN Web Docs