JavaScript Tip: Use .at() for Cleaner Array Access

💡 JavaScript Tip: Start using the .at(-1) today! If you're still accessing the last element of an array like this: arr[arr.length - 1] There’s a cleaner and more readable way 👇 arr.at(-1) 🔹 Why use .at()? ✅ Cleaner syntax ✅ Easier to read ✅ Supports negative indexing 🔹 Examples const nums = [10, 20, 30, 40]; nums.at(0); // 10 nums.at(2); // 30 nums.at(-1); // 40 (last element) nums.at(-2); // 30 🔹 When to use it? Accessing last or second-last elements Writing cleaner, more modern JavaScript Avoiding repetitive .length calculations ⚠️ Note .at() works in modern JavaScript (ES2022+), so ensure your environment supports it. Small improvements like this can make your code more readable and elegant ✨ Are you using .at() already? #JavaScript #CleanCode #WebDevelopment #FrontendDevelopment #ProgrammingTips #DevCommunity #SoftwareEngineering

  • graphical user interface, website

To view or add a comment, sign in

Explore content categories