Saturday, November 16, 2024
HomeSoftware DevelopmentArray.prototype.at

Array.prototype.at

[ad_1]

Working with arrays is an important ability in any programming language, particularly JavaScript, as we proceed to depend on exterior knowledge APIs. JavaScript has added strategies like discover and `findIndex just lately, however one syntax I really like from languages like Python is retrieving values by damaging indexes.

Once you wish to get the worth of the final merchandise in an array, you find yourself with an archaic expression:

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

You might use pop however that modifies the array. As an alternative you need to use at and an index, even a damaging 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 little or no identified perform however helpful, if just for the shorthand syntax!

  • Create a Sheen Logo Effect with CSS
  • Convert XML to JSON with JavaScript
  • HTML5 Context Menus

    One of many hidden gems throughout the HTML5 spec is context menus. The HTML5 context menu spec permits builders to create customized context menus for given blocks inside easy menu and menuitem components. The menu info lives proper throughout the web page so…

  • Fullscreen API

    As we transfer towards extra true net functions, our JavaScript APIs are doing their finest to maintain up.  One quite simple however helpful new JavaScript API is the Fullscreen API.  The Fullscreen API offers a programmatic solution to request fullscreen show from the consumer, and exit…


[ad_2]
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments