I deal with négative a and negative n too
//best perf, hard to read function modul3(a,n){ r = a/n | 0 ; if(a < 0){ r += n < 0 ? 1 : -1 } return a - n * r } // shorter code function modul(a,n){ return a%n + (a < 0 && Math.abs(n)); } //beetween perf and small code function modul(a,n){ return a - n * Math[n > 0 ? 'floor' : 'ceil'](a/n); }