JS Tip: Simplify Objects with Property Shorthand

🎯 JS Tip: Stop Repeating Variable Names for Object Keys! 🎯 When the name of the object property should be the same as the variable holding its value, use Property Shorthand to avoid redundancy. ❌ The Old Way (Redundant Key/Value Names) js code - var id = 123; var name = 'Alex'; var isActive = true; // Key and value variable names are identical, creating repetition var user = {   id: id,   name: name,   isActive: isActive }; // user is { id: 123, name: 'Alex', isActive: true } --- ✅ The New Way (Property Shorthand) js code - const id = 123; const name = 'Alex'; const isActive = true; // Simply write the variable name; JavaScript assumes the property key is the same const user = {   id, // Same as id: id   name, // Same as name: name   isActive, // Same as isActive: isActive   createdAt: Date.now() }; // user is { id: 123, name: 'Alex', isActive: true, createdAt: 1733031050518 } ---- 🔥 Why it's better: It's a significant improvement in conciseness and readability. It removes unnecessary repetition, making objects much cleaner to define, especially when constructing data structures from existing variables. ------^------ 👉 View My New Services - https://lnkd.in/gGjgKsB5 🎇 Do you want more tips like this? Do follow and write "Yes" in the comment box. #javascriptTips #JavaScript #JSTips #ES6 #Developer #Programming #WebDev #ReactJS #NodeJS #Coding #TechTips #WebDeveloper #MdRedoyKayser #Webxpanda #WordPress

  • text

Property shorthand is one of those small ES6 features that makes a big difference in readability and maintainability. Especially useful when building objects from state, props, or API payloads. Clean code starts with reducing unnecessary repetition.

To view or add a comment, sign in

Explore content categories