Yevhen Klymentiev
dark
light
console
darkness
y.klymentiev@gmail.com
Reusable Snippets|Practical utility code for everyday use — custom-built and ready to share

isEven

Checks if a number is even.

TypeScript
Copied!
1/**
2 * Checks if a number is even.
3 *
4 * @param num - The number to check.
5 * @returns True if the number is even, otherwise false.
6 */
7export function isEven(num: number): boolean {
8  return num % 2 === 0;
9}
  • Extremely Simple and Fast

    Uses a single modulus operation for immediate evaluation with near-zero overhead.

  • High Readability

    Self-explanatory logic makes the function easy to understand and maintain.

  • Consistent Boolean Return

    Always returns a strict boolean, which is reliable in conditional logic and comparisons.

Tests | Examples

TypeScript
Copied!
1test('isEven - positive even number', () => {
2  expect(isEven(4)).toBe(true);
3});
4
5test('isEven - positive odd number', () => {
6  expect(isEven(5)).toBe(false);
7});
8
9test('isEven - zero is even', () => {
10  expect(isEven(0)).toBe(true);
11});
12
13test('isEven - negative even number', () => {
14  expect(isEven(-2)).toBe(true);
15});
16
17test('isEven - negative odd number', () => {
18  expect(isEven(-3)).toBe(false);
19});
20
21test('isEven - float number', () => {
22  expect(isEven(2.2)).toBe(false);
23});

Common Use Cases

  • Alternating Logic

    Apply different behaviors on even/odd cycles, e.g., row shading in tables or animations.

  • Game Mechanics

    Trigger effects, bonuses, or state changes based on turn parity or move count.

  • Index-Based Conditions

    Filter or process array elements differently based on whether their index is even.

  • Validation Rules

    Check if numeric inputs (like counts, codes, or identifiers) meet even-value constraints.

Codebase: Utilities -> Numbers -> isEven | Yevhen Klymentiev