Validate the integer first, then return n % 2 !== 0. Negative values need a nonzero comparison.
Use n % 2 !== 0 after checking that n is a safe integer. The remainder expression answers the parity question; the guard prevents strings, fractions, and rounded large values from quietly becoming answers. This small function is useful anywhere your input contract promises an ordinary JavaScript integer.
A helper for safe integers
function isOdd(n) {
if (!Number.isSafeInteger(n)) {
throw new TypeError('Expected a safe integer');
}
return n % 2 !== 0;
}
console.log(isOdd(27)); // true
console.log(isOdd(-27)); // true
console.log(isOdd(0)); // false
console.log(isOdd(-0)); // false
Handle negative integers
JavaScript's % operator keeps the dividend's sign, so a negative odd integer produces a negative remainder. For example, -27 % 2 is -1. Comparing that result with 1 would incorrectly reject the number. Comparing with zero handles either sign. MDN documents the remainder rules.
Reject invalid input before arithmetic
The guard is part of this function's contract. A fraction such as 2.5 has a nonzero remainder but is neither an odd integer nor an even integer. Similarly, NaN % 2 !== 0 evaluates to true, making an unguarded expression a poor validator. Throwing separates invalid input from the legitimate answer false.
Number.isSafeInteger accepts integer values between negative and positive 9,007,199,254,740,991, inclusive. It does not convert a numeric string before checking it. Its reference explains the safe range. If your identifiers can exceed that range, preserve their original decimal text and use BigInt instead.
For a form field, treat parsing as a separate decision. You might accept surrounding whitespace, or you might insist on digits only. Calling Number without that decision can turn an empty string into zero. Calling parseInt can accept an initial integer while leaving trailing text unexamined. Neither behavior belongs implicitly in a parity helper.
Test and use the helper
Here is a compact check of the intended behavior:
for (const [n, expected] of [[-5, true], [-4, false], [0, false], [9, true]]) {
console.assert(isOdd(n) === expected);
}
// Each call below throws:
// isOdd('9'); isOdd(9.5); isOdd(Infinity);
Use the helper when filtering a validated numeric list, assigning alternating integer slots, or validating an odd count. If invalid records should be skipped, perform that filtering explicitly rather than catching every error and calling it even. A boolean result should mean that a supported integer was checked successfully.
The expression does not need a network request or an npm dependency. Choose readable arithmetic first. A claim that a bitwise spelling is faster requires measurement in the actual workload, and it would not remove the need to validate input.
Boxed Number objects are rejected too. If a legacy API supplies them, unwrap the value deliberately at its boundary so object conversion does not become an accidental feature of the predicate.