Definition
n = 2k
Even means divisible by 2 with remainder 0.
An even number is divisible by 2 with no remainder. This tutorial covers modulo and bitwise checks, zero and negatives, a live preview, worked JavaScript examples, edge cases, and complexity.
n = 2k
Even means divisible by 2 with remainder 0.
n % 2 === 0
Clearest beginner and interview-friendly check.
(n & 1) == 0
Optional shortcut using the least significant bit.
(0 % 2) === 0
A common interview trivia point — say it clearly.
Try any n
Classify positive and negative integers instantly.
Single check
One modulo or bitwise test; ranges cost O(N).
Even numbers are integers divisible by 2 with no leftover. Mathematically: n = 2 * k for some integer k. Examples: 10, 0, and -4 are even; 11 is odd.
In JavaScript the clearest test is n % 2 === 0. You can also use (n & 1) == 0 as a bitwise shortcut.
Parity checks appear everywhere — loops, filters, indexing, and interview warm-ups — so mastering them early pays off.
n % 2 === 0 is the clearest test.
0 is even — a favorite trivia question.
-4 % 2 == 0 in JavaScript.
Mention (n & 1) == 0 after modulo.
In short: if n % 2 === 0, the integer is even — including 0 and many negatives.
Given an integer n, decide whether it is even (divisible by 2).
// 10 → 10 % 2 === 0 → even
// 11 → 11 % 2 === 1 → odd
// 0 → 0 % 2 === 0 → even (zero is even) | Item | Type | Description |
|---|---|---|
n | number | Any integer (positive, negative, or zero). |
| Return / print | boolean / text | true if n is even. |
function isEven(n) {
return n % 2 === 0;
} | Method | Idea | Notes |
|---|---|---|
| Modulo | n % 2 === 0 | Best default for interviews |
| Bitwise | (n & 1) == 0 | LSB is 0 for even integers |
| Step-by-two | Start at first even, add 2 | Faster range listing |
| Goal | Pattern |
|---|---|
| Even test | n % 2 === 0 |
| Odd test | n % 2 != 0 |
| Bitwise even | (n & 1) == 0 |
| Inclusive range | for (i = start; i <= end; i++) |
| Classic yes | 0, 10, -4 |
| Classic no | 11, -3 |
Three ways to work with even numbers — pick by clarity and use case.
n % 2 === 0Clearest for beginners and interviews
(n & 1) == 0Looks at the least significant bit
for (i = a; i <= b; i += 2)List evens without testing each value
modulo firstThen mention bitwise as a follow-up
Reach for even checks whenever parity matters.
Often the first control-flow and modulo question.
Keep only even indices or even values.
Print all even numbers between a and b.
Makes remainder-by-2 concrete with 10 vs 11.
Confirm that 0 is even before coding harder problems.
Key benefit: one O(1) check that unlocks filters, range printers, and clearer interview answers about zero.
Works with positive and negative integers in JavaScript safe range.
Three complete JavaScript programs — modulo check, range scan, and bitwise parity. Click View Output to reveal sample console results.
The classic remainder-by-2 helper.
Simple helper function for single-value parity checking.
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.`);
} The function returns true when the remainder by 2 is exactly zero.
Reuse the helper to filter an inclusive interval.
Reuses the same helper and prints only even values.
function isEven(num) {
return num % 2 === 0;
}
function printEvensInRange(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());
}
printEvensInRange(1, 10); Inclusive ranges use i <= end in a for loop. For speed on large intervals, start at the first even and step by 2.
Use the least significant bit as a parity shortcut.
Even integers have LSB 0, so n & 1 is 0.
function isEvenBitwise(number) {
return (number & 1) === 0;
}
for (const n of [10, 11, 0, -4]) {
const label = isEvenBitwise(n) ? "even" : "odd";
console.log(`${n}: ${label}`);
} Bitwise AND with 1 isolates the least significant bit. Prefer modulo in interviews for readability; offer bitwise as an optional follow-up.
Find n % 2 (or check n & 1).
If remainder is 0, the number is even.
Apply the same check for each value in a range.
Remainder 0 → even; otherwise odd.
n = 10Trace the modulo method for a classic even value.
| Step | Expression | Result |
|---|---|---|
| 1 | 10 % 2 | 0 |
| 2 | 0 === 0 | true |
| 3 | Classify | Even |
For contrast, 11 % 2 is 1 → odd.
Where even checks show up beyond the interview prompt.
First-day modulo and boolean practice.
Example: write isEven(n).
Keep only even values or even indices.
Example: [x for x in nums if x % 2 == 0].
List all evens between a and b.
Example: 1 to 10 → 2 4 6 8 10.
Toggle behavior on even/odd steps.
Example: zebra-stripe rows in a table.
Show LSB understanding with & 1.
Example: compare modulo and bitwise results.
Confirm 0 is even before harder number theory.
Example: ask “is zero even?” first.
Pro Tip: say “zero is even” out loud before coding — interviewers love that clarity.
Why this pattern works well in interviews and classwork.
One sentence: divisible by 2 with remainder 0.
A single modulo or bitwise operation decides parity.
The same isEven powers single checks and range scans.
Bitwise, step-by-two ranges, and zero trivia come for free.
Pro Tip: lead with modulo; offer bitwise only after the interviewer asks about alternatives.
Small habits that keep parity solutions interview-ready.
It reads like the definition: remainder zero.
State that 0 is even before writing code.
Check -4 and -3 so sign does not surprise you.
Use i <= end when the end bound should be included.
Start at the first even and increment by 2 for large ranges.
Pro Tip: convert user input with int(...) safely before any parity check.
Mistakes that commonly break even-number solutions.
Some beginners assume 0 is neither even nor odd.
→ 0 is even: (0 % 2) === 0.
Forgetting end + 1 drops the last value.
→ Use for (i = start; i <= end; i++) for inclusive ends.
Assuming only positives need testing.
→ Verify with -4 and -3.
Jumping to & 1 without explaining modulo.
→ Start with %, then mention bitwise.
Passing floats or strings without conversion.
→ Parse to int first (or reject invalid input).
The formula works for all integers, including negatives and zero.
n = 0Even by definition.
-4 % 2 == 0, so -4 is even.
Remember i <= end in loop bounds.
Convert user input to integer safely before checking.
Start from the first even and increment by 2.
n % 2 == 1Odd means remainder 1 (for nonnegative; negatives still work with %).
Handy follow-ups interviewers sometimes ask.
% still yields a non-negative remainder, so -4 % 2 == 0.Try these variations to lock in the pattern.
n % 2 === 0 to test evenness.Quick Takeaway: if the remainder when dividing by 2 is 0, the integer is even.
| Operation | Time | Extra space |
|---|---|---|
| Single check | O(1) | O(1) |
Range scan [a, b] | O(b - a + 1) | O(1) |
| Step-by-two listing | O((b - a) / 2) | O(1) |
An even integer is divisible by 2 with remainder 0. Use n % 2 === 0 for clarity, mention (n & 1) == 0 as a bitwise option, and remember that zero is even.
Practice the three examples above, then continue to evil numbers for a bit-count twist on parity.
Prefer modulo in interviews, test 0 and negatives, and use step-by-two when listing large ranges.
n % 2 === 0end + 1 in rangesCheck parity the interview-friendly way.
n % 2 === 0
Definition0 is even
Trivia(n & 1) == 0
Option-4 is even
EdgeO(1) check
AnalysisZero is an even number because 0 = 2 * 0. In JavaScript, (0 % 2) === 0 is true.
Learn how evil numbers use an even count of set bits in binary.
9 people found this page helpful