Skip to content
VersionSize

isOdd

The isOdd utility is a type guard that checks if a given number is an odd integer.

Implementation

View Source Code
ts
/**
 * Checks if a number is odd.
 *
 * @param {number} arg - The number to check.
 *
 * @returns {boolean} - Returns true if the number is odd, otherwise false.
 */
export function isOdd(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 not divisible by 2.
  • Type-safe: Properly typed for numeric inputs.

API

ts
function isOdd(value: number): boolean;

Parameters

  • value: The number to check.

Returns

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

Examples

Basic Usage

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

isOdd(3); // true
isOdd(41); // true
isOdd(2); // false
isOdd(0); // false

Implementation Notes

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

See Also

  • isEven: Check if a number is even.
  • isNumber: Check if a value is a number.
  • isWithin: Check if a number is within a range.