How to check if a number is odd in JavaScript

Checking if numbers are odd is essential for alternating patterns, conditional logic, special case handling, and implementing features like unique styling or selective processing in JavaScript applications. With over 25 years of experience in software development and as the creator of CoreUI, I’ve implemented odd number checking extensively in components like grid layouts, list processing, and pattern-based styling where identifying non-even numbers enables specific behaviors and visual effects. From my extensive expertise, the most direct and mathematically accurate approach is using the modulo operator (%) to test for a remainder when divided by 2. This method is efficient, clear, and directly represents the mathematical definition of odd numbers.

Use the modulo operator (%) to check if a number has a remainder when divided by 2.

const number = 5
const isOdd = number % 2 === 1
// Result: true

const checkOdd = (num) => num % 2 !== 0
// Usage: checkOdd(7) returns true, checkOdd(8) returns false

The modulo operator % returns the remainder after division. When a number is divided by 2, odd numbers always have a remainder of 1, while even numbers have no remainder. In this example, 5 % 2 equals 1, so 5 % 2 === 1 returns true. Alternatively, you can check number % 2 !== 0 which is more general and works correctly with negative numbers. For negative odd numbers like -3, the remainder might be -1, so !== 0 is more reliable than === 1.

Best Practice Note:

This is the same approach we use in CoreUI components for implementing alternating patterns, conditional rendering, and special case handling across our component ecosystem. The !== 0 comparison is preferred over === 1 because it handles negative numbers correctly. For performance-critical code, use bitwise AND: (number & 1) === 1. The modulo approach provides better readability and is sufficient for most applications requiring odd number detection.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author