The ECMAScript proposal “.item() (by Shu-yu Guo and Tab Atkins) introduces the mentioned method for indexable values (Arrays, Typed Arrays, strings). Given an index, the method returns the corresponding element. Its key benefit is that indices can be negative (-1 gets the last element, -2 the second last, etc.). This blog post examines .item() in detail.


Table of contents:

  • Method item() for indexable classes
  • Out-of-bounds indices
  • Classes that have method .item()
  • Accessing the elements at the end of an Array – alternatives to .item()
  • A polyfill for .item()
  • npm packages with polyfills
  • .item() and upgrading indexable DOM collections
  • Example: .replace() with callback
  • FAQ
  • Why not allow negative indices in brackets?
  • Why not .getItem() and .setItem()?
  • What about the ECMAScript proposal for .last?
  • Sources and acknowledgements

#javascript #ecmascript #es6

ECMAScript proposal: Method <code>.item()</code> for Arrays, Typed Arrays, and strings
4.85 GEEK