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.

Give it a spin

Zero dependencies · CommonJS + ESM · TypeScript ready

01 / THE PLAYGROUND

See the cycle in action.

Change the numbers. Follow the loop.

YOUR PARAMETERS
positions

Exclusive upper bound. 5 means indexes 0–4.

start here
move by
ONE LINE. THAT'S IT.(curr + inc) % max
A LITTLE ROUND TRIP 5 positions
NEXT INDEX0back to the beginning ↻
CurrentNextTap a number to start there
YOUR JOURNEY

4

TAKE IT FOR A SPIN
Runs the actual package bundle. Works offline.

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)
cyclicNext parameters
ParameterDefaultWhat it does
maxrequiredNumber of positions in the cycle
curr0Your current position
inc1How 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-prev
const cyclicNext = require('cyclic-next');
const cyclicPrev = require('cyclic-prev');

cyclicNext(5, 4); // 0: forward to the first
cyclicPrev(5, 0); // 4: backward to the last

Both 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).

cyclic-prev documentation ↗ · cyclic-prev on npm ↗

Keep things moving.

A small, MIT-licensed utility. Built to fit right in.

Get cyclic-next on npm ↗