๐ Mastering JavaScript Object Properties: Dot Notation vs. Bracket Notation (With Emojis, Digits and More!)

๐งฑ JavaScript Objects = Key-Value Pairs
Youโve probably used this:
myObject.name = "Ken";
Thatโs dot notation โ the simplest way to access object properties. But hereโs the thing:
โ It only works for valid identifiers (letters, numbers,
_,$). โ It canโt handle spaces, emojis, or special characters.
๐คฏ What Happens with Weird Property Names?
Say you try this:
myObject.special property โบ = 'Oops!';
Youโll get a syntax error. Why? That property name has a space and an emoji.
โ Use Bracket Notation Instead
Hereโs the fix:
myObject['special property โบ'] = 'It works!';
console.log(myObject['special property โบ']); // Output: 'It works!'
Bracket notation accepts any string, making it perfect for:
- User-generated property names
 - Property names with emojis, spaces, punctuation, etc.
 
๐ข What About Numbers as Keys?
You can do:
myObject[123] = 'hi!';
JavaScript converts the number 123 to the string '123', so this works:
console.log(myObject['123']); // hi!
console.log(myObject[123]);   // hi!
You can even use expressions:
myObject['12' + '3'];   // hi!
myObject[120 + 3];      // hi!
myObject[123.0];        // hi!
โ ๏ธ Avoid leading zeros in number keys:
myObject[0123]; // Might behave strangely (octal!)
๐ง Key Takeaways
- Dot Notation โ For normal names like 
name,age,user_1. - 
    
Bracket Notation โ For:
- Special characters (
['hello world!']) - Emojis (
['๐ป']) - All-digit names (
[123]) - Dynamic expressions (
['key' + 1]) 
 - Special characters (
 
โจ Final Tip
Use dot notation for clean, predictable keys. Use bracket notation when things get fancy, weird, or dynamic.
Here are 3 review questions you can add at the end of your post:
๐ Review Questions
- 
    
Why canโt you use dot notation to access a property like
myObject["user name"]? (Hint: Think about valid identifiers.) - 
    
What will the following code output?
const obj = {}; obj[456] = 'Hello!'; console.log(obj['456']);(Explain why.)
 - 
    
When should you choose bracket notation over dot notation in JavaScript? (List at least two cases.)