JavaScript Odd Number Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is an Odd Number?

An integer is odd when it is not divisible by 2. Equivalently, n % 2 is not 0.

Remember
n is odd  ↔  n % 2 !== 0

Examples:
  11 % 2 = 1  → odd
   7 % 2 = 1  → odd
  10 % 2 = 0  → even (not odd)
   0 % 2 = 0  → even (not odd)
nn % 2Result
111Odd
100Even
00Even
-5-1Odd

How to Check an Odd Number in JavaScript

Four clear steps — then pick modulo, bitwise, or a step-by-two range loop.

  1. Read the integer n (positive, negative, or zero).
  2. Compute n % 2 (or check the least significant bit with n & 1).
  3. If the remainder is not 0 (or the LSB is 1), n is odd.
  4. To list odds in a range, test each value — or start at the first odd and add 2.
MethodIdeaTimeBest for
Modulon % 2 !== 0O(1)Interviews, clearest logic
Bitwise(n & 1) !== 0O(1)Optional follow-up, LSB check
Step by 2Start at first odd, add 2O(N/2)Listing odds in a range

Pseudocode

Pseudocode
function IsOdd(n):
    return (n mod 2) != 0

function PrintOdds(start, end):
    for i from start to end:
        if IsOdd(i):
            output i

// Faster range listing:
function PrintOddsStep(start, end):
    i = start
    if i is even:
        i = i + 1
    while i <= end:
        output i
        i = i + 2

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Odd testn % 2 !== 0
Even testn % 2 === 0
Bitwise odd(n & 1) !== 0
Zero0 is even — not odd
Negatives-5 is odd; -4 is even
Classic check11 → odd; 10 → even

Live Preview

Enter any integer to see the remainder and the odd/even verdict.

Whole numbers only. Tap a chip or type a value.

Result
n = 11
n % 2 = 1
(n & 1) = 1
Verdict: ODD

JavaScript Programs

Start with the modulo helper — it is the clearest interview answer. Then reuse it for a range, and finish with the bitwise shortcut. Each example has a Try it Yourself editor.

Example 1 — Modulo Check

Return true when the remainder by 2 is not zero.

JavaScript
function isOdd(n) {
  return n % 2 !== 0;
}

const number = 11;
if (isOdd(number)) {
  console.log(number + " is an odd number.");
} else {
  console.log(number + " is not an odd number.");
}
Try It Yourself

How It Works

  1. isOdd computes n % 2 and checks that it is not 0.
  2. For 11, the remainder is 1, so the function returns true.
  3. The caller prints the matching message based on that boolean.
  4. Swap 11 for 10 to see the even (not odd) path.
  5. This is the default answer to give in interviews — clear and correct.

Example 2 — Odds in a Range

Reuse the helper to print every odd value from 1 to 10.

JavaScript
function isOdd(n) {
  return n % 2 !== 0;
}

const start = 1;
const end = 10;

console.log("Odd numbers from " + start + " to " + end + ":");
let line = "";
for (let i = start; i <= end; i++) {
  if (isOdd(i)) {
    line += i + " ";
  }
}
console.log(line.trim());
Try It Yourself

How It Works

  1. The loop runs from start to end inclusive (i <= end).
  2. Each i is tested with isOdd.
  3. Only odd values are collected: 1 3 5 7 9.
  4. Even values like 2, 4, and 6 are skipped.
  5. For large ranges, start at the first odd and use i += 2 instead of testing every integer.

Example 3 — Bitwise Check

Odd integers have a least significant bit of 1, so n & 1 is not 0.

JavaScript
function isOddBitwise(n) {
  return (n & 1) !== 0;
}

const samples = [11, 10, 0, -5];
for (const n of samples) {
  const label = isOddBitwise(n) ? "odd" : "even";
  console.log(n + ": " + label);
}
Try It Yourself

How It Works

  1. n & 1 keeps only the least significant bit.
  2. If that bit is 1, the number is odd; if 0, it is even.
  3. The sample array shows positives, zero, and a negative odd value.
  4. Results match the modulo method for these integers.
  5. Prefer modulo in interviews for readability; mention bitwise as a follow-up.

Edge Cases & Pitfalls

Check these before calling the solution done.

Zero

0 is not odd

0 % 2 === 0, so zero is even. Do not treat it as “neither.”

Negatives

Parity still applies

-5 % 2 is -1 in JavaScript — still nonzero, so still odd under !== 0.

Floats

Use integers

Odd/even is defined for integers. Do not apply % 2 to floating-point values without a clear rule.

Range

Inclusive bounds

Use i <= end when both ends should be checked. Off-by-one errors drop the last odd.

Time and Space Complexity

ProgramTimeExtra space
Modulo / bitwise checkO(1)O(1)
Scan range of size NO(N)O(1)
Step-by-two listingO(N/2)O(1)

A single parity test is constant time. Range printers grow with how many integers you visit.

Key Takeaways

  • Definition: not divisible by 2 → n % 2 !== 0.
  • Zero: 0 is even, so it is not odd.
  • Bitwise: (n & 1) !== 0 checks the LSB.
  • Test set: 11 → odd; 10 → even; 0 → even; -5 → odd.

One line: an integer is odd when n % 2 !== 0 (or its least significant bit is 1).

Frequently Asked Questions

An integer that is not divisible by 2. For nonnegative values, the remainder when divided by 2 is 1.
No. Zero is even because 0 % 2 equals 0.
Use n % 2 !== 0, or (n & 1) !== 0 for a bitwise least-significant-bit check.
Yes. In JavaScript, -5 % 2 equals -1, which is nonzero, so n % 2 !== 0 correctly treats -5 as odd.
A single check is O(1). Listing odds in a range of size N is O(N), or about O(N/2) if you step by 2.
Use the Try it Yourself links under each code sample — they open an in-browser editor with the same logic so you can edit the input and Run.

Next: Palindrome Number

Check whether a number reads the same forwards and backwards.

Palindrome tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful