Dev Helpers

JavaScript / Units and time

Normalize a compass bearing

Dev Helpers · August 17, 2026

JavaScript's remainder can be negative. Normalize a bearing into one full turn before choosing a compass label.

Worked example

const bearing = -35;
console.log(((bearing % 360) + 360) % 360);

Result

325

Implementation notes

Normalizing 360 to zero keeps lookup indices in bounds. If the UI prints 360 for north, make that a presentation choice after the calculation.

Try it yourself

Compare -1, 359, 360 and 721 degrees.

Open the JavaScript example →

Editorial date August 17, 2026 · Published December 6, 2026

Related articles

Sum distances with a Euclidean norm

A diagonal is longer than either of its components. Math.hypot combines perpendicular distances without making the caller square each input.

Build an OS-independent path

Path conventions belong to the destination. Use an explicit path flavour when generating portable asset names.

Resolve ranked ties

A leaderboard needs an explicit tie policy. Ranking tied scores and sorting their display order are separate decisions.