- Remainder
9 % 2 = 1
Check Odd Number in JavaScript
What you’ll learn
- What “odd” means when you divide by two (leftover of one for nonnegative integers).
- A tiny
isOddhelper usingn % 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 %.
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.
1515 = 2 · 7 + 1, remainder 1 when divided by 2—odd.
Quick examples
- 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.
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
function isOdd(n):
return (n mod 2) != 0
function printOddsInRange(start, end):
for i from start to end:
if isOdd(i):
output in % 2 !== 0
Helper returns true or false in JavaScript; matches the classic interview style with 15.
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.");
}Explanation
isOdd is true when there is any nonzero remainder mod 2. Change number to experiment (14 prints the else branch).
Odds in [1, 10]
Same idea as the reference: walk the range and print 1 3 5 7 9.
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();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
🔄 Input / output examples
Swap number in Example 1 or extend Example 2 with parameters start and end if you want a flexible range.
| n | Odd? |
|---|---|
0 | No (even) |
15 | Yes |
22 | No |
-5 | Yes (not divisible by 2) |
Edge cases
The phrase “not odd” includes evens and zero—do not confuse “not odd” with “even and positive.”
n = 0
0 % 2 === 0, so isOdd(0) is false.
Signed integers
Odd negatives still satisfy n % 2 !== 0 in JavaScript; the inequality test still works for parity checks.
i <= end
Keep both endpoints when the problem says “from 1 to 10” inclusively.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
isOdd(n) | O(1) | O(1) |
Range [a, b] | O(b - a + 1) | O(1) |
| Step-by-two odd loop | half as many iterations | O(1) |
No heap allocation is required for these snippets.
Summary
- Test:
n % 2 !== 0(or(n & 1) !== 0once you are ready). - Zero is even, so it is not odd.
- Ranges: reuse the same test inside a
forloop.
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.
8 people found this page helpful
