How to check if a number is odd in JavaScript
Checking if a number is odd is essential for alternating patterns, conditional rendering, selective processing, and special-case logic in JavaScript applications. With over 25 years of experience in software development and as the creator of CoreUI, I’ve used odd/even checks in grid layouts, list processing, and pattern-based styling throughout the component library. For the companion check, see how to check if a number is even in JavaScript.
Use the modulo operator % to test if a number has a remainder when divided by 2.
5 % 2 !== 0 // true — odd
4 % 2 !== 0 // false — even
-3 % 2 !== 0 // true — negative odd (remainder is -1, not 1)
1 % 2 !== 0 // true
0 % 2 !== 0 // false — zero is even
1. Basic Odd Check
Use !== 0 rather than === 1 — in JavaScript, the modulo of a negative odd number is negative (e.g. -3 % 2 === -1), so === 1 produces a wrong result for negatives.
const isOdd = n => n % 2 !== 0
isOdd(1) // true
isOdd(3) // true
isOdd(0) // false
isOdd(-3) // true — correct with !== 0
isOdd(-4) // false
2. Bitwise Approach (Performance-Critical Code)
Bitwise AND directly inspects the least significant bit — an odd number always has it set to 1.
const isOddBitwise = n => (n & 1) === 1
isOddBitwise(3) // true — binary 011, last bit 1
isOddBitwise(4) // false — binary 100, last bit 0
isOddBitwise(-3) // true — two's complement, last bit 1
In practice, the performance difference is negligible for UI workloads. Prefer % 2 !== 0 for readability.
3. Filtering Odd Numbers from an Array
const numbers = [1, 2, 3, 4, 5, 6, 7, 8]
const odds = numbers.filter(n => n % 2 !== 0)
// [1, 3, 5, 7]
const oddIndexed = numbers.filter((_, i) => i % 2 !== 0)
// [2, 4, 6, 8] — elements at odd indices (1, 3, 5, 7)
Filtering by index position is the standard pattern for extracting alternate items from a list, e.g. taking every second row from a dataset.
4. Alternating Layouts — Odd Rows
Odd-index detection drives common UI patterns like alternating row styles, indent toggling, and two-column alternating layouts.
const items = ['Alice', 'Bob', 'Carol', 'Dave', 'Eve']
items.forEach((name, index) => {
const el = document.createElement('div')
el.className = index % 2 !== 0 ? 'item-odd' : 'item-even'
el.textContent = name
list.appendChild(el)
})
CoreUI’s CTable component handles zebra-striping via a striped prop automatically — but the underlying logic is exactly this pattern.
5. Special-Case Processing on Odd Steps
Odd checks are useful when you need to act on every other iteration — for example, inserting a separator between items.
function renderWithSeparators(items) {
return items.reduce((acc, item, i) => {
acc.push(item)
if (i % 2 !== 0 && i < items.length - 1) {
acc.push('---')
}
return acc
}, [])
}
renderWithSeparators(['A', 'B', 'C', 'D', 'E'])
// ['A', 'B', '---', 'C', 'D', '---', 'E']
6. Handling Floating-Point Inputs
Like the even check, guard against floats when the input comes from user input or an API.
function isOddSafe(n) {
if (!Number.isInteger(n)) return false
return n % 2 !== 0
}
isOddSafe(3) // true
isOddSafe(3.0) // true — 3.0 is an integer in JS
isOddSafe(3.5) // false — correctly rejects float
isOddSafe(-7) // true
Best Practice Note:
Always use n % 2 !== 0 rather than n % 2 === 1 — the latter silently fails for negative odd numbers.
Add a Number.isInteger guard whenever the input may come from user input or external data.
For performance-sensitive loops, the bitwise (n & 1) === 1 is equivalent and marginally faster, but readability usually wins.



