Toolbox-XToolbox-X
GuardsChecker Functions

Number Checkers

Predicate functions to check/compare numbers.

Predicate functions to check/compare numbers. These functions return true or false.

isPrime

Determines whether a given number is a prime number using an efficient 6k ± 1 optimization.

Function Signature

isPrime(number: number): boolean;

Parameters

  • number — The integer to evaluate for prime number check.

Returns

  • true if the number is a prime, otherwise false.

Examples

playground.ts

Notes

  • Uses a fast prime number check based on:
    • Simple divisibility rules for 2 and 3.
    • 6k ± 1 optimization to reduce unnecessary checks.
  • Returns false for all numbers below 2.
  • Ideal for performance on small to moderate integers.

isEven

Checks if a number is even (divisible by 2).

Function Signature

isEven(input: Numeric): boolean;

Parameters

  • input: The number or numeric string to check.

Returns

Returns true if the number is even, otherwise false.

Notes

  • Handles both number and numeric string inputs.
  • Returns false for non-numeric strings and non-finite numbers or NaN.

Example

playground.ts

isOdd

Checks if a number is odd (not divisible by 2).

Function Signature

isOdd(input: Numeric): boolean;

Parameters

  • input: The number or numeric string to check.

Returns

Returns true if the number is odd, otherwise false.

Notes

  • Handles both number and numeric string inputs.
  • Returns false for non-numeric strings and non-finite numbers or NaN.

Example

playground.ts

isMultiple

Checks if a number is a multiple of another number.

Function Signature

isMultiple(input: number, multipleOf: number): boolean;

Parameters

  • input: The number to check.
  • multipleOf: The potential multiple.

Returns

Returns true if input is a multiple of multipleOf, otherwise false.

Example

playground.ts

Notes

  • Returns true when input is 0 and multipleOf is 0 (mathematically undefined case)
  • Handles negative numbers correctly

isPerfectSquare

Checks if a number is a perfect square.

Function Signature

isPerfectSquare(num: number): boolean;

Parameters

  • num: The number to check.

Returns

Returns true if the number is a perfect square, otherwise false.

Example

playground.ts

isFibonacci

Checks if a number is part of the Fibonacci sequence.

Function Signature

isFibonacci(num: number): boolean;

Parameters

  • num: The number to check.

Returns

Returns true if the number is in the Fibonacci sequence, otherwise false.

Example

playground.ts

Algorithm

Uses the mathematical property that a number n is Fibonacci if and only if one or both of (5*n^2 + 4) or (5*n^2 - 4) is a perfect square.


areInvalidNumbers

Checks if any input is not a finite number.

Function Signature

areInvalidNumbers(...numbers: number[]): boolean;

Parameters

  • numbers: Spread of numbers to validate.

Returns

Returns true if any input is not finite (NaN, Infinity, etc.), otherwise false.

Example

playground.ts

Common Use Cases

  • Validating user input
  • Sanity checking before mathematical operations
  • Data quality verification

Notes

  • Returns true for:
    • NaN
    • Infinity
    • -Infinity
    • Non-number values
  • Returns false for all finite numbers including zero

Aliases

Function NameAlias
isPrimeisPrimeNumber
isEvenisEvenNumber
isOddisOddNumber
isMultipleisMultipleOf
isFibonacciisPartOfFibonacci, isPartOfFibonacciSeries
areInvalidNumbersareNumbersInvalid, isInvalidNumber, isNumberInvalid

Last updated: Tue, Jun 09, 2026 04:51:46AM (UTC)

On this page