Use Array.filter(Boolean) to Clean Arrays in JavaScript

Hey LinkedIn Family 👋 A small JavaScript habit that improved how I handle arrays: 🚀 Use Array.filter(Boolean) to clean data quickly. Earlier, I used to write: const values = ["React", "", null, "JavaScript", undefined]; const clean = values.filter(item => item); Now I prefer: const values = ["React", "", null, "JavaScript", undefined]; const clean = values.filter(Boolean); Result 👇 ["React", "JavaScript"] Why this works well ✅ Cleaner and more expressive ✅ Removes falsy values (null, undefined, "", 0, false) ✅ Very useful for API responses and dynamic data Real-world usage 👇 const tags = [user?.skill1, user?.skill2, user?.skill3].filter(Boolean); ⚠️ Watch out This also removes 0 and false. If those are valid values in your data, this can introduce subtle bugs. Biggest Lesson: Good JavaScript is not just about shortcuts… It’s about knowing when to use them and when not to. What’s a small JS trick you use that saved you from a bug? 👇 #JavaScript #ReactJS #ReactNative #Programming #SoftwareEngineering #CodingTips #DeveloperLife

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories