THE SHORT ANSWER

Mathematical modulo and JavaScript remainder agree for positive integers but differ for negative ones. A validated integer is odd when its remainder by two is nonzero.

Positive examples hide the difference

For a positive integer such as 7, both mathematical modulo and JavaScript’s remainder produce 1 when dividing by two. That makes n % 2 === 1 look like a complete odd-number test. Negative integers expose the missing case.

In the usual mathematical convention with positive divisor two, −7 mod 2 = 1. JavaScript evaluates -7 % 2 to -1. Both calculations can be internally consistent because they choose different integer quotients.

Two valid equations, different conventions

The mathematical division algorithm requires a remainder at least zero and less than the positive divisor. Therefore:

−7 = 2 × (−4) + 1.

Here the quotient is −4 and the remainder is 1. This is the convention defined in the Barrus and Clark division-algorithm chapter.

JavaScript’s remainder follows the dividend’s sign, using the quotient truncated toward zero:

−7 = 2 × (−3) − 1.

The quotient is −3, leaving −1. MDN documents this behavior for %. Calling the operator modulo without explaining the convention is where many otherwise sensible examples go wrong.

Test the property you actually need

For parity, you do not need to normalize the remainder into a positive value. You only need to know whether division by two leaves zero:

function isOddInteger(n) {
  if (!Number.isSafeInteger(n)) {
    throw new TypeError('Expected a safe integer');
  }
  return n % 2 !== 0;
}

This returns true for 7 and −7, and false for 8, −8, and 0. The safe-integer guard also rejects fractions, infinities, and numeric values whose integer precision should not be trusted.

With an existing BigInt, use n % 2n !== 0n. Keep the operands in the same numeric type. The formula is the same; the literal syntax differs.

When normalization is useful

If you need a canonical mathematical residue, such as an index in a two-position cycle, use ((n % 2) + 2) % 2 for validated integer inputs. That converts a negative remainder of −1 into 1.

Do not use a nonzero remainder as a parity test for arbitrary values. For example, 2.5 % 2 is nonzero, but 2.5 is not an odd integer. The input check is part of the algorithm.

Try positive and negative counterparts in the calculator, and read negative-number parity for the direct mathematical proof.

Sources & further reading

Want to check a number?Try the calculator ↗