A LITTLE UTILITY WITH A LOT OF USES
Small function.
Endless possibilities.
Move to the next index. Reach the end. Start again.
A tiny JavaScript helper for things that go around.
Zero dependencies · CommonJS + ESM · TypeScript ready
01 / THE PLAYGROUND
See the cycle in action.
Change the numbers. Follow the loop.
(curr + inc) % max4
v1 preserves JavaScript's remainder behavior. A negative result stays negative; it is not normalized into the cycle.
02 / A FEW GOOD USES
Put your loops to work.
Less index math. More building.
A carousel that never runs out of slides.
03 / THE WHOLE API
Three inputs.
One next index.
Use a positive integer cycle size and a non-negative current index for array indexing.
Read the full documentation ↗cyclicNext(max, curr = 0, inc = 1)| Parameter | Default | What it does |
|---|---|---|
max | required | Number of positions in the cycle |
curr | 0 | Your current position |
inc | 1 | How far to move forward |
Returns (curr + inc) % max. Negative values retain their sign; a zero bound returns NaN. v1 keeps this behavior for compatibility. The visual playground limits cycle sizes to 1–24; the library does not.
04 / THE COMPANION
Need to go backward?
cyclic-prev moves to the previous index and wraps from the first position to the last.
Try the cyclic-prev playground ↺npm install cyclic-next cyclic-prevconst cyclicNext = require('cyclic-next');
const cyclicPrev = require('cyclic-prev');
cyclicNext(5, 4); // 0: forward to the first
cyclicPrev(5, 0); // 4: backward to the lastBoth packages ship CommonJS, ES modules, and TypeScript declarations with zero runtime dependencies. Their edge cases differ: cyclic-next v1 preserves JavaScript remainder behavior; cyclic-prev validates safe integers and normalizes the result into [0, max).
Keep things moving.
A small, MIT-licensed utility. Built to fit right in.
Get cyclic-next on npm ↗