JavaScript Improvements: Modern Array Methods for Cleaner Code

🚀 JavaScript just got smarter (again)… are you keeping up? Most developers are still catching up with ES6… But JavaScript has moved way ahead. Here are some latest JS features that can actually level up your code 👇 🔥 𝟭. 𝗔𝗿𝗿𝗮𝘆.𝗽𝗿𝗼𝘁𝗼𝘁𝘆𝗽𝗲.𝘁𝗼𝗦𝗼𝗿𝘁𝗲𝗱() (𝗡𝗼 𝗺𝘂𝘁𝗮𝘁𝗶𝗼𝗻!) We’ve all done this: arr.sort() Problem? It mutates the original array 😬 Now: const sorted = arr.toSorted(); ✅ No side effects ✅ Cleaner functional approach 🔥 𝟮. 𝗔𝗿𝗿𝗮𝘆.𝗽𝗿𝗼𝘁𝗼𝘁𝘆𝗽𝗲.𝘁𝗼𝗥𝗲𝘃𝗲𝗿𝘀𝗲𝗱() Instead of: arr.reverse() Use: const reversed = arr.toReversed(); 👉 Original array stays untouched (finally!) 🔥 𝟯. 𝗔𝗿𝗿𝗮𝘆.𝗽𝗿𝗼𝘁𝗼𝘁𝘆𝗽𝗲.𝘁𝗼𝗦𝗽𝗹𝗶𝗰𝗲𝗱() Replace this messy mutation: arr.splice(1, 2, 'new') With: const newArr = arr.toSpliced(1, 2, 'new'); 💡 Immutable + predictable state (React devs… you’ll love this) 🔥 𝟰. 𝗢𝗯𝗷𝗲𝗰𝘁.𝗵𝗮𝘀𝗢𝘄𝗻() (𝗕𝗲𝘁𝘁𝗲𝗿 𝘁𝗵𝗮𝗻 𝗵𝗮𝘀𝗢𝘄𝗻𝗣𝗿𝗼𝗽𝗲𝗿𝘁𝘆) Old way: obj.hasOwnProperty('key') Modern way: Object.hasOwn(obj, 'key') ✅ Safer ✅ Cleaner ✅ No prototype issues 🔥 𝟱. 𝗧𝗼𝗽-𝗹𝗲𝘃𝗲𝗹 𝗮𝘄𝗮𝗶𝘁 No more wrapping in async functions: const data = await fetch(url); 🚀 Makes scripts and modules much cleaner 🔥 𝟲. 𝗳𝗶𝗻𝗱𝗟𝗮𝘀𝘁() & 𝗳𝗶𝗻𝗱𝗟𝗮𝘀𝘁𝗜𝗻𝗱𝗲𝘅() Find from the end of the array: arr.findLast(x => x > 10); 💡 Super useful in real-world data processing ⚠️ Reality check: Most devs don’t fail because they don’t know JS… They fail because they write outdated JS. 💡 If you're serious about growth: Start writing modern, immutable, predictable JavaScript Follow for more real-world dev insights 🚀 #javascript #webdevelopment #frontend #reactjs #softwareengineering #programming #coding #developers #365DaysOfCode #DAY80

  • JavaScript, web developer

This is such a timely reminder! Switching from arr.sort() to arr.toSorted() was a small change but it completely changed how I think about immutability. The findLast() tip is something I hadn't explored yet — adding it to my practice list right away. 🚀

To view or add a comment, sign in

Explore content categories