Check Odd Number in JavaScript

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 2 Code Examples
Parity

What you’ll learn

  • What “odd” means when you divide by two (leftover of one for nonnegative integers).
  • A tiny isOdd helper using n % 2 !== 0, plus printing odds between 1 and 10.
  • A live preview and why zero is not odd.

Overview

Odd numbers are the other half of the number line alongside evens: if splitting a pile into two equal whole rows leaves exactly one left over, the count is odd. In JavaScript you detect that with the remainder of division by 2.

Two programs

One value (15) and a fixed range 1–10.

Live preview

Type an integer; see odd vs not odd with the same rule as the JavaScript samples.

Plain facts

Zero is even, so it fails the “odd” test—by design.

Prerequisites

The modulo operator %, if, and for loops.

  • Basic JavaScript syntax, functions, and console.log.
  • Knowing that division can leave a remainder (that is what % gives you).

What is an odd number?

For counting numbers 1, 2, 3, …, an integer is odd when it is not divisible by 2 without remainder. Practically: divide by two; if there is a leftover unit, the number is odd (1, 3, 5, 7, …).

In code you rarely prove it with words; you ask JavaScript for the remainder using %.

Odd n % 2 !== 0
Even n % 2 === 0
Zero even

Remainder mod 2

Odd integers are those congruent to 1 modulo 2 (in the usual nonnegative story). Even integers are congruent to 0. So “odd” and “even” chop the integers into two teams.

15

15 = 2 · 7 + 1, remainder 1 when divided by 2—odd.

Quick examples

9 Odd
Remainder
9 % 2 = 1
8 Even
Remainder
8 % 2 = 0

Takeaway: same symbol %, opposite branch from the even-number lesson.

Live preview

Uses JavaScript safe integers. The rule is odd when n % 2 !== 0.

Try 0 (not odd), 7, or -3.

Live result
Press “Is it odd?” to see the verdict.

Algorithm

Goal: return true iff n is not divisible by 2 (equivalently, remainder nonzero).

Compute n % 2

If the remainder is not 0, treat n as odd (for typical beginner nonnegative inputs this is simply remainder 1).

Range scan

Loop from start to end; print values that pass the odd test.

📜 Pseudocode

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

function printOddsInRange(start, end):
    for i from start to end:
        if isOdd(i):
            output i
1

n % 2 !== 0

Helper returns true or false in JavaScript; matches the classic interview style with 15.

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

const number = 15;

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

Explanation

isOdd is true when there is any nonzero remainder mod 2. Change number to experiment (14 prints the else branch).

2

Odds in [1, 10]

Same idea as the reference: walk the range and print 1 3 5 7 9.

JavaScript
function printOddNumbersFrom1To10() {
  const values = [];

  for (let i = 1; i <= 10; i++) {
    if (i % 2 !== 0) {
      values.push(String(i));
    }
  }

  console.log("Odd numbers in the range 1 to 10:");
  console.log(values.join(" "));
}

printOddNumbersFrom1To10();
Try it Yourself

Explanation

You could also loop for (let i = 1; i <= 10; i += 2) for odds only—fewer iterations, same printed line when start is odd.

Notes

Step by two. To list only odds in [start, end], advance i by 2 after aligning start to the next odd if needed.

Bit trick. (n & 1) !== 0 detects odd integers in JavaScript; prefer % until you are comfortable.

Interview: mention that 0 is even, and that isOdd and isEven partition the integers.

❓ FAQ

It is a whole number that cannot be split into two equal whole piles without a leftover&mdash;dividing it by 2 leaves remainder 1 (for nonnegative integers). Examples: 1, 3, 15, 101.
No. Zero counts as even (you can write 0 = 2 &middot; 0). So “odd” tests such as n % 2 !== 0 correctly classify 0 as not odd.
The remainder when dividing by 2 is 0 for evens and nonzero for odds. For negatives, n % 2 !== 0 still correctly means not divisible by 2 for odd integers.
For integers, yes: exactly one of the two is true. In code, isOdd can be (n % 2 !== 0) and isEven can be (n % 2 === 0).
On two’s complement, the least significant bit is 1 for odd integers. It is a common micro-optimization; n % 2 !== 0 is usually clearer for beginners.
A single test is O(1) time and O(1) space. Scanning a range [a, b] is O(b - a + 1) with O(1) extra space.

🔄 Input / output examples

Swap number in Example 1 or extend Example 2 with parameters start and end if you want a flexible range.

nOdd?
0No (even)
15Yes
22No
-5Yes (not divisible by 2)

Edge cases

The phrase “not odd” includes evens and zero—do not confuse “not odd” with “even and positive.”

Zero

n = 0

0 % 2 === 0, so isOdd(0) is false.

Negatives

Signed integers

Odd negatives still satisfy n % 2 !== 0 in JavaScript; the inequality test still works for parity checks.

Inclusive range

i <= end

Keep both endpoints when the problem says “from 1 to 10” inclusively.

⏱️ Time and space complexity

OperationTimeExtra space
isOdd(n)O(1)O(1)
Range [a, b]O(b - a + 1)O(1)
Step-by-two odd loophalf as many iterationsO(1)

No heap allocation is required for these snippets.

Summary

  • Test: n % 2 !== 0 (or (n & 1) !== 0 once you are ready).
  • Zero is even, so it is not odd.
  • Ranges: reuse the same test inside a for loop.
Did you know?

Every whole number is either even or odd—never both, never neither. Zero is even, so the test n % 2 !== 0 correctly says zero is not odd.

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.

8 people found this page helpful