Check Even Number in JavaScript

Beginner
⏱️ 7 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Parity

What You’ll Learn

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.

Definition

n = 2k

Even means divisible by 2 with remainder 0.

Modulo Test

n % 2 === 0

Clearest beginner and interview-friendly check.

Bitwise

(n & 1) == 0

Optional shortcut using the least significant bit.

Zero Is Even

(0 % 2) === 0

A common interview trivia point — say it clearly.

Live Preview

Try any n

Classify positive and negative integers instantly.

O(1)

Single check

One modulo or bitwise test; ranges cost O(N).

Introduction

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.

Why it matters?

Parity checks appear everywhere — loops, filters, indexing, and interview warm-ups — so mastering them early pays off.

Key Highlights

Modulo First

n % 2 === 0 is the clearest test.

Zero Counts

0 is even — a favorite trivia question.

Negatives Too

-4 % 2 == 0 in JavaScript.

Bitwise Option

Mention (n & 1) == 0 after modulo.

In short: if n % 2 === 0, the integer is even — including 0 and many negatives.

📝 Problem & Approach

Given an integer n, decide whether it is even (divisible by 2).

JavaScript
// 10  → 10 % 2 === 0  → even
// 11  → 11 % 2 === 1  → odd
// 0   → 0 % 2 === 0   → even (zero is even)

Inputs & Outputs

ItemTypeDescription
nnumberAny integer (positive, negative, or zero).
Return / printboolean / texttrue if n is even.

Minimal workflow

Pseudocode
function isEven(n) {
  return n % 2 === 0;
}

Method comparison

MethodIdeaNotes
Modulon % 2 === 0Best default for interviews
Bitwise(n & 1) == 0LSB is 0 for even integers
Step-by-twoStart at first even, add 2Faster range listing

⚡ Quick Reference

GoalPattern
Even testn % 2 === 0
Odd testn % 2 != 0
Bitwise even(n & 1) == 0
Inclusive rangefor (i = start; i <= end; i++)
Classic yes0, 10, -4
Classic no11, -3

📋 Modulo vs Bitwise vs Step-by-Two

Three ways to work with even numbers — pick by clarity and use case.

Modulo
n % 2 === 0

Clearest for beginners and interviews

Bitwise
(n & 1) == 0

Looks at the least significant bit

Step by 2
for (i = a; i <= b; i += 2)

List evens without testing each value

Interview tip
modulo first

Then mention bitwise as a follow-up

Context

When This Problem Shows Up

Reach for even checks whenever parity matters.

  1. Interview warm-ups

    Often the first control-flow and modulo question.

  2. Filtering lists

    Keep only even indices or even values.

  3. Range listing tasks

    Print all even numbers between a and b.

  4. Teaching modulo

    Makes remainder-by-2 concrete with 10 vs 11.

  5. Zero trivia

    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.

🔮 Live Preview

Works with positive and negative integers in JavaScript safe range.

Try 10, 11, 0, or -4.

Live result
Press "Check parity" to classify.

Examples Gallery

Three complete JavaScript programs — modulo check, range scan, and bitwise parity. Click View Output to reveal sample console results.

📚 Getting Started

The classic remainder-by-2 helper.

Example 1 — Check One Number with Modulo

Simple helper function for single-value parity checking.

JavaScript
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.`);
}

How It Works

The function returns true when the remainder by 2 is exactly zero.

⚡ Range Output

Reuse the helper to filter an inclusive interval.

Example 2 — Print Evens in Range [1, 10]

Reuses the same helper and prints only even values.

JavaScript
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);

How It Works

Inclusive ranges use i <= end in a for loop. For speed on large intervals, start at the first even and step by 2.

⚙️ Bitwise Style

Use the least significant bit as a parity shortcut.

Example 3 — Bitwise Even Check

Even integers have LSB 0, so n & 1 is 0.

JavaScript
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}`);
}

How It Works

Bitwise AND with 1 isolates the least significant bit. Prefer modulo in interviews for readability; offer bitwise as an optional follow-up.

🧠 How the Algorithm Decides

1

Compute remainder

Find n % 2 (or check n & 1).

Setup
2

Check zero

If remainder is 0, the number is even.

Test
3

Reuse in a loop

Apply the same check for each value in a range.

Scan
=

Even or odd

Remainder 0 → even; otherwise odd.

🔎 Worked Walkthrough — n = 10

Trace the modulo method for a classic even value.

StepExpressionResult
110 % 20
20 === 0true
3ClassifyEven

For contrast, 11 % 2 is 1 → odd.

Use Cases

Where even checks show up beyond the interview prompt.

1. Interview Warm-Ups

First-day modulo and boolean practice.

Example: write isEven(n).

2. List Filters

Keep only even values or even indices.

Example: [x for x in nums if x % 2 == 0].

3. Range Printers

List all evens between a and b.

Example: 1 to 10 → 2 4 6 8 10.

4. Alternating Logic

Toggle behavior on even/odd steps.

Example: zebra-stripe rows in a table.

5. Bitwise Follow-Ups

Show LSB understanding with & 1.

Example: compare modulo and bitwise results.

6. Zero Trivia

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.

Advantages

Why this pattern works well in interviews and classwork.

  1. 1. Tiny Definition

    One sentence: divisible by 2 with remainder 0.

  2. 2. O(1) Check

    A single modulo or bitwise operation decides parity.

  3. 3. Reusable Helper

    The same isEven powers single checks and range scans.

  4. 4. Natural Follow-Ups

    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.

Usage Tips

Small habits that keep parity solutions interview-ready.

  1. 1. Prefer Modulo First

    It reads like the definition: remainder zero.

  2. 2. Call Out Zero

    State that 0 is even before writing code.

  3. 3. Test Negatives

    Check -4 and -3 so sign does not surprise you.

  4. 4. Inclusive Ranges

    Use i <= end when the end bound should be included.

  5. 5. Step by Two When Listing

    Start at the first even and increment by 2 for large ranges.

Pro Tip: convert user input with int(...) safely before any parity check.

Common Pitfalls

Mistakes that commonly break even-number solutions.

  1. 1. Calling Zero Odd

    Some beginners assume 0 is neither even nor odd.

    → 0 is even: (0 % 2) === 0.

  2. 2. Exclusive Range End

    Forgetting end + 1 drops the last value.

    → Use for (i = start; i <= end; i++) for inclusive ends.

  3. 3. Skipping Negatives

    Assuming only positives need testing.

    → Verify with -4 and -3.

  4. 4. Leading with Bitwise Only

    Jumping to & 1 without explaining modulo.

    → Start with %, then mention bitwise.

  5. 5. Non-Integer Input

    Passing floats or strings without conversion.

    → Parse to int first (or reject invalid input).

Edge Cases

The formula works for all integers, including negatives and zero.

Zero

n = 0

Even by definition.

Negative

Still valid

-4 % 2 == 0, so -4 is even.

Range bounds

Inclusive end

Remember i <= end in loop bounds.

Input

Validate types

Convert user input to integer safely before checking.

Large range

Step by 2

Start from the first even and increment by 2.

Odd contrast

n % 2 == 1

Odd means remainder 1 (for nonnegative; negatives still work with %).

⚖️ Facts Worth Knowing

Handy follow-ups interviewers sometimes ask.

  • Modulo 2. Even numbers are congruent to 0 modulo 2; odd numbers to 1.
  • Zero. 0 = 2 × 0, so 0 is even.
  • LSB. Even integers end with binary bit 0.
  • JavaScript remainder. For negatives, % still yields a non-negative remainder, so -4 % 2 == 0.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Classify classics

  • 0, 10, -4 → even
  • 11, -3 → odd

2. Match both styles

  • Modulo vs bitwise
  • Assert identical booleans

3. Range 1 to 20

  • Print all evens
  • Then rewrite with step 2

4. Count evens

  • Count even values in a list
  • Do not mutate the list

Notes

  • Rule: use n % 2 === 0 to test evenness.
  • Remember: zero is even, and negatives can be even too.
  • Ranges: reuse the same helper; step by 2 when listing many values.
  • Single check is O(1); range scan is O(range size).

Quick Takeaway: if the remainder when dividing by 2 is 0, the integer is even.

⏱️ Time and Space Complexity

OperationTimeExtra space
Single checkO(1)O(1)
Range scan [a, b]O(b - a + 1)O(1)
Step-by-two listingO((b - a) / 2)O(1)
Wrap Up

🎉 Conclusion

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.

💡 Best Practices

✅ Do

  • Lead with n % 2 === 0
  • State that zero is even
  • Test negatives like -4
  • Use inclusive end + 1 in ranges
  • Mention bitwise as a follow-up

❌ Don’t

  • Call zero neither even nor odd
  • Forget inclusive range ends
  • Skip negative test cases
  • Lead only with bitwise tricks
  • Pass unparsed user strings

Key Takeaways

Knowledge Unlocked

Five things to remember about even numbers

Check parity the interview-friendly way.

5
Core concepts
0 02

Zero

0 is even

Trivia
& 03

Bitwise

(n & 1) == 0

Option
- 04

Negatives

-4 is even

Edge
O 05

Complexity

O(1) check

Analysis

❓ Frequently Asked Questions

An integer n is even if n can be written as 2*k for some integer k.
Yes. 0 is divisible by 2 and (0 % 2) === 0 in JavaScript.
Because remainder 0 means divisible by 2. It is the clearest beginner test.
Yes, (n & 1) === 0 also detects even numbers for safe integers in bitwise operations.
Yes. For example, (-4 % 2) === 0, so -4 is even. Odd negatives yield -1, not +1.
Checking one number is O(1). Scanning a range is O(range size).
Prefer modulo in interviews for clarity; mention bitwise as an optional shortcut.
Start from the first even value and step by 2 instead of testing every integer.
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.

Did you Know? 🔊

Zero is an even number because 0 = 2 * 0. In JavaScript, (0 % 2) === 0 is true.

Continue to Evil Number

Learn how evil numbers use an even count of set bits in binary.

Evil number 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.

9 people found this page helpful