- Pairs
- four groups of two
Check Even Number in JavaScript
What you’ll learn
- The definition of even (including zero) and the usual JavaScript test
n % 2 === 0. - An
isEvenhelper plus printing evens in a closed interval[start, end]. - Live preview and notes on signed remainder in JavaScript.
Overview
Parity splits the integers into evens and odds. The simplest predicate is number % 2 === 0, which returns a boolean you can use in if or aggregate in a loop.
Two programs
One value (10) and a range 1–10 listing.
Live preview
Safe-range integers; shows parity with the same rule as the samples.
Rigor
Explicit note on zero and JavaScript % with negatives.
Prerequisites
The remainder operator %, if, for loops, and console.log.
functiondeclarations,const/let, strict equality===.- Optional: bitwise
&as an alternative parity test (32-bit).
What is an even number?
An integer n is even iff n = 2k for some integer k. On the number line, evens are spaced by two: …, -4, -2, 0, 2, 4, ….
Checking divisibility by 2 is the standard program task; it is the same predicate used inside many “print every other element” loops.
Congruence mod 2
Even integers are exactly those congruent to 0 modulo 2: n ≡ 0 (mod 2). Odd integers are congruent to 1 (mod 2) in the usual residue system.
1010 = 2 · 5, remainder 0 upon division by 2.
Intuition
- Remainder
7 % 2 = 1
Takeaway: parity is the first nontrivial modular filter after divisibility by every integer.
Live preview
Integers in the JavaScript safe range. Uses n % 2 === 0 (same idea as the sample programs).
Algorithm
Goal: return true iff n is divisible by 2.
Compute n % 2
If the remainder is 0, n is even; otherwise odd (for signed n, mind JavaScript remainder sign).
Range scan
For each i in [start, end], apply the same predicate and collect matches.
📜 Pseudocode
function isEven(n):
return (n mod 2) = 0
function printEvensInRange(start, end):
for i from start to end:
if isEven(i):
output in % 2 === 0
Minimal helper returning a boolean, matching the reference structure.
function isEven(number) {
return number % 2 === 0;
}
const number = 10;
if (isEven(number)) {
console.log(number + " is an even number.");
} else {
console.log(number + " is not an even number.");
}Explanation
isEven returns true or false; if (isEven(number)) branches on that boolean.
Evens in [1, 10]
Same output as the reference range demo: 2 4 6 8 10. Reuses n % 2 === 0 inside the loop.
function isEven(num) {
return num % 2 === 0;
}
function checkEvenNumbersInRange(start, end) {
console.log("Even numbers in the range " + start + " to " + end + ":");
let line = "";
for (let i = start; i <= end; i++) {
if (isEven(i)) {
line += i + " ";
}
}
console.log(line.trim());
}
const start = 1;
const end = 10;
checkEvenNumbersInRange(start, end);Explanation
The loop walks every integer in the inclusive range; isEven filters to multiples of two. An alternative micro-optimization is (i & 1) === 0 on 32-bit bitwise paths.
Optimization
Step by two. If you only need evens, iterate for (let i = start + (start & 1); i <= end; i += 2) after aligning start to the next even (bitwise only safe within 32-bit range).
Bitwise parity. (n & 1) === 0 matches n % 2 === 0 for integers that fit the bitwise conversion rules.
Interview: mention 0, ECMAScript remainder sign on negatives, and the bitwise alternative.
❓ FAQ
🔄 Input / output examples
Change number in Example 1 or start/end in Example 2.
| n | Even? |
|---|---|
0 | Yes |
10 | Yes |
11 | No |
-4 | Yes (-4 % 2 === 0) |
Edge cases and pitfalls
Treating “not even” as always meaning “odd” is fine for integers; for negative odds, % yields -1 in JavaScript.
n = 0
Even by definition; 0 % 2 === 0 is true.
Number.MIN_SAFE_INTEGER
It is even (ends in 8); parity via % still holds for safe integers.
2n
For huge integers, n % 2n === 0n is the same test in BigInt space.
Inclusive range
Use i <= end when the problem says “through end” inclusively.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
isEven(n) | O(1) | O(1) |
Range [a, b] | O(b - a + 1) | O(1) |
| Step-by-two loop | O((b-a)/2) iterations | O(1) |
No auxiliary memory is required beyond loop indices and the output string.
Summary
- Test:
n % 2 === 0or(n & 1) === 0(bitwise caveats). - Zero is even; range scans reuse the same predicate.
- Watch-outs: JavaScript remainder sign on negatives; inclusive loop bounds.
In modern mathematics zero is even: it is an integer multiple of 2 (0 = 2 · 0). In JavaScript, (0 % 2) === 0 is true.
8 people found this page helpful
