Skip to content
VersionSize

isNegative

The isNegative utility is a type guard that checks if a given number is strictly less than zero.

Implementation

View Source Code
ts
/**
 * @description
 * Checks if the value is a negative number.
 *
 * @example
 * ```ts
 * isNegative(-123); // true
 * isNegative(123); // false
 * isNegative(0); // false
 * isNegative(0.1); // false
 * isNegative(-0.1); // true
 * isNegative('hello world'); // false
 * isNegative({}); // false
 * isNegative([]); // false
 * isNegative(new Date()); // false
 * isNegative(null); // false
 * isNegative(undefined); // false
 * isNegative(NaN); // false
 * ```
 *
 * @param arg - The argument to be checked.
 *
 * @returns `true` if the value is a negative number, else `false`.
 */
export function isNegative(arg: unknown): arg is number {
  return typeof arg === 'number' && arg < 0;
}

Features

  • Isomorphic: Works in both Browser and Node.js.
  • Strict Check: Only returns true for values $< 0$ (zero is not considered negative).
  • Type-safe: Properly typed for numeric inputs.

API

ts
function isNegative(value: number): boolean;

Parameters

  • value: The number to check.

Returns

  • true if the number is less than zero; otherwise, false.

Examples

Basic Usage

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

isNegative(-10); // true
isNegative(-0.1); // true
isNegative(0); // false
isNegative(5); // false

Implementation Notes

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

See Also

  • isPositive: Check if a number is greater than zero.
  • isZero: Check if a number is exactly zero.
  • isNumber: Check if a value is a number.