JavaScript Optional Chaining and Nullish Coalescing

Hey LinkedIn Family 👋 A small JavaScript habit that improved the way I write code: 🚀 Use optional chaining (?.) and nullish coalescing (??) properly. Earlier, I used to write: const city = user && user.address && user.address.city ? user.address.city : "Unknown"; Now I write: const city = user?.address?.city ?? "Unknown"; Why this is better 👇 ✅ Cleaner code ✅ Easier to read ✅ Safer when data is missing ✅ Great for API responses This becomes very useful in React / React Native apps where nested data is common. Example: const profileImage = response?.data?.user?.avatar ?? defaultImage; Biggest Lesson: Good JavaScript is often about writing simpler code, not more code. Small improvements in syntax can make a big difference in maintainability. What JavaScript feature do you use the most these days? 👇 #JavaScript #WebDevelopment #ReactJS #ReactNative #Programming #SoftwareEngineering #CodingTips #DeveloperLife

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories