Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
JavaScript .at() Method: The Modern Way to Access Array & String Elements (#84)
Play lesson

🔥 JavaScript Mastery Course (2026) | Modern JavaScript ES6+ from Beginner to Advanced - JavaScript .at() Method: The Modern Way to Access Array & String Elements (#84)

5.0 (0)
6 learners

What you'll learn

This course includes

  • 28.5 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

Tired of using array[array.length - 1] just to get the last item in a list? The .at() method (introduced in ES2022) is the elegant solution you’ve been waiting for. In this tutorial, we explain the mechanics of how this method simplifies element access for both arrays and strings, specifically highlighting the power of negative indexing.👉 We’ll break down why this is a game-changer for writing cleaner, more readable code. What you will learn: ✔️ Modern Syntax: How to use .at() on both Arrays and Strings. ✔️ Negative Indexing: Accessing elements from the end of a collection without complex math. ✔️ Strings vs. Arrays: Seeing how the behavior remains consistent across different data types. ✔️ Clean Code: Replacing clunky legacy indexing with concise ES2022 syntax. ✔️ Practical Use Case: Refactoring a name-formatting function to be more readable. ✔️ Immutability: Confirming that .at() never changes your original data. 🕛 Timelines: 0:00 Introduction to the .at() Method 0:13 Basics: Accessing elements by positive index 0:50 The Killer Feature: Negative Indexing (-1, -2) 1:13 Practical Coding: Array Examples 2:42 Using .at() on Strings 4:20 Real-World Case Study: Refactoring formatName() 6:00 Comparison: .at() vs. Substring 6:40 Summary and Browser Compatibility 💡 The "2026 Best Practice" Tip As noted in the lecture, .at() is now fully supported across all modern browsers and environments. In 2026, it is considered the "gold standard" for accessing the end of an array. While square brackets [] are still great for simple, positive indices (like arr[0]), always reach for .at(-1) when you need the last element to make your code instantly more understandable to other developers! 💡 Peer Tip: Remember that .at() returns undefined if you provide an index that is out of bounds (like an index larger than the array length). It won't throw an error, so keep a "null check" in mind if your array size is unpredictable! Expert Guide Question: Now that you know how to use .at(-1) to get the last character of a string, can you think of a situation where this would be more useful than using a regular for loop?

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere