Check Even Number in JavaScript

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 2 Code examples + Try it
Parity

What you’ll learn

  • The definition of even (including zero) and the usual JavaScript test n % 2 === 0.
  • An isEven helper 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.

  • function declarations, 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.

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

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.

10

10 = 2 · 5, remainder 0 upon division by 2.

Intuition

8 Even
Pairs
four groups of two
7 Odd
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).

Try 0, -4, or 11.

Live result
Press “Check parity” to classify.

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

Pseudocode
function isEven(n):
    return (n mod 2) = 0

function printEvensInRange(start, end):
    for i from start to end:
        if isEven(i):
            output i
1

n % 2 === 0

Minimal helper returning a boolean, matching the reference structure.

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.");
}
Try it Yourself

Explanation

isEven returns true or false; if (isEven(number)) branches on that boolean.

2

Evens in [1, 10]

Same output as the reference range demo: 2 4 6 8 10. Reuses n % 2 === 0 inside the loop.

JavaScript
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);
Try it Yourself

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

An integer n is even if there exists an integer k with n = 2k. Equivalently, n is divisible by 2 with remainder 0.
Yes: 0 = 2·0. The test n % 2 === 0 returns true for n = 0 in JavaScript.
The remainder of division by 2 is 0 for even integers. For odd integers the remainder is ±1 depending on the sign of n (JavaScript uses truncated division toward zero, so the remainder matches the dividend’s sign).
On two's complement, (n & 1) === 0 detects even integers for safe integers in bitwise operations. Parity via % is clearer for beginners; mind that bitwise ops coerce to 32-bit signed ints.
Even negatives such as -4 still satisfy n % 2 === 0. For odd negatives, n % 2 is -1 (not +1), but comparing to 0 still separates evens from odds.
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

Change number in Example 1 or start/end in Example 2.

nEven?
0Yes
10Yes
11No
-4Yes (-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.

Zero

n = 0

Even by definition; 0 % 2 === 0 is true.

MIN_SAFE

Number.MIN_SAFE_INTEGER

It is even (ends in 8); parity via % still holds for safe integers.

BigInt

2n

For huge integers, n % 2n === 0n is the same test in BigInt space.

Off-by-one

Inclusive range

Use i <= end when the problem says “through end” inclusively.

⏱️ Time and space complexity

OperationTimeExtra space
isEven(n)O(1)O(1)
Range [a, b]O(b - a + 1)O(1)
Step-by-two loopO((b-a)/2) iterationsO(1)

No auxiliary memory is required beyond loop indices and the output string.

Summary

  • Test: n % 2 === 0 or (n & 1) === 0 (bitwise caveats).
  • Zero is even; range scans reuse the same predicate.
  • Watch-outs: JavaScript remainder sign on negatives; inclusive loop bounds.
Did you know?

In modern mathematics zero is even: it is an integer multiple of 2 (0 = 2 · 0). In JavaScript, (0 % 2) === 0 is true.

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