isZero
The isZero utility is a type guard that checks if a given number is exactly equal to zero.
Implementation
View Source Code
ts
/**
* Checks if the value is a positive number.
*
* @example
* ```ts
* isPositive(0); // true
* isPositive(123); // false
* isPositive(-123); // false
* isPositive(0.0000001); // false
* isPositive('hello world'); // false
* isPositive({}); // false
* isPositive([]); // false
* isPositive(new Date()); // false
* isPositive(null); // false
* isPositive(undefined); // false
* isPositive(NaN); // false
* ```
*
* @param arg - The argument to be checked.
*
* @returns `true` if the value is a positive number, else `false`.
*
*/
export function isZero(arg: unknown): arg is number {
return typeof arg === 'number' && arg === 0;
}Features
- Isomorphic: Works in both Browser and Node.js.
- Strict Equality: Only returns
truefor the numeric value0. - Type-safe: Properly typed for numeric inputs.
API
ts
function isZero(value: number): boolean;Parameters
value: The number to check.
Returns
trueif the number is zero; otherwise,false.
Examples
Basic Usage
ts
import { isZero } from '@vielzeug/toolkit';
isZero(0); // true
isZero(-0); // true (JavaScript considers 0 and -0 equal)
isZero(1); // false
isZero(0.1); // falseImplementation Notes
- Returns
trueifvalue === 0. - Throws nothing; returns
falsefor non-numeric inputs if TypeScript checks are bypassed.
See Also
- isPositive: Check if a number is greater than zero.
- isNegative: Check if a number is less than zero.
- isNumber: Check if a value is a number.