TIL is my collection of short, technical daily learnings. 235 and counting.
JavaScript Wraparound Index
What I call “wraparound” are repeatable indices on an array. The common use case is a carousel. Here’s how it’s done: const items = ['first', 'middle', 'last']; const i = 0; // Our iterable index // "Next" -> const nextIndex = (i + 1) % items.length; // <- "Previous" const prevIndex = (i - 1 + items.length) % items.length;