Array.prototype.at


Working with arrays is an important skill in any programming language, especially JavaScript, as we continue to rely on external data APIs. JavaScript has added methods like find and `findIndex recently, but a syntax I love from languages ​​like Python is to retrieve values ​​by negative indexes.

When you want the value of the last element in an array, you end up with an archaic expression:

const arr = ["zero", "one", "two", "three"];
const last = arr[arr.length - 1];

You could use pop but it changes the array. Instead, you can use at and an index, even a negative index, to retrieve values:

const arr = ["zero", "one", "two", "three"];
arr.at(-1); // "three"
arr.at(-2); // "two"
arr.at(0); // "zero"

at is a very little known feature, but useful, albeit only for the shorthand syntax!

Monitoring the performance of the website
Monitoring the performance of the website
Monitoring the performance of the website
  • Camera and video control with HTML5

    Camera and video control with HTML5

    Client-side APIs on mobile and desktop devices quickly deliver the same APIs. Of course, our mobile devices first gained access to some of these APIs, but these APIs are slowly making their way to the desktop. One of these APIs is the getUserMedia API …

  • Create a CSS cube

    Create a CSS cube

    CSS cubes really show what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative images. Add animation and you have something really nice. Unfortunately, every CSS cube tutorial I’ve read is a bit …

  • CSS Selection Styling

    CSS Selection Styling

    The goal of CSS is to allow styling of content and structure on a web page. We all know that, do we? As CSS revisions arrive, we get more options to check. One of the little known styling options available in the browser is text selection styling.

  • How to create a Twitter card

Leave a Comment