Skip to content
VersionSize

isEven

The isEven utility is a type guard that checks if a given number is an even integer.

Implementation

View Source Code
ts
/**
 * Checks if a number is even.
 *
 * @param {number} arg - The number to check.
 *
 * @returns {boolean} - Returns true if the number is even, otherwise false.
 */
export function isEven(arg: unknown): arg is number {
  return typeof arg === 'number' && Number.isFinite(arg) && Number.isInteger(arg) && arg % 2 === 0;
}

Features

  • Isomorphic: Works in both Browser and Node.js.
  • Strict Check: Only returns true for valid numbers that are divisible by 2.
  • Type-safe: Properly typed for numeric inputs.

API

ts
function isEven(value: number): boolean;

Parameters

  • value: The number to check.

Returns

  • true if the number is even; otherwise, false.

Examples

Basic Usage

ts
import { isEven } from '@vielzeug/toolkit';

isEven(2); // true
isEven(42); // true
isEven(3); // false
isEven(0); // true

Implementation Notes

  • Returns true if value % 2 === 0.
  • Throws nothing; returns false for non-numeric inputs if TypeScript checks are bypassed.

See Also

  • isOdd: Check if a number is odd.
  • isNumber: Check if a value is a number.
  • isWithin: Check if a number is within a range.