Skip to content

isNonEmpty

Checks if a string is non-empty (has at least one character). null and undefined are considered empty and return false.

Whitespace-only strings are considered non-empty. Use isNonEmpty(value?.trim()) if you need to exclude blank strings.

Available since v2.0.3

import { isNonEmpty } from '@helpers4/string';
// or, from the all-in-one package (same code, one install):
import { isNonEmpty } from 'helpers4/string';
isNonEmpty(value: string | null | undefined): value is string
ParameterTypeDescription
valuestring | null | undefinedThe string to check

value is string — true if the string has at least one character; false for "", null, or undefined. Acts as a type guard: the if branch narrows string | null | undefined to string.

Returns true for any string with at least one character, including whitespace.

isNonEmpty('hello') // => true
isNonEmpty(' ')     // => true  (whitespace is content)
isNonEmpty('')      // => false

Compose with .trim() when whitespace-only strings should be treated as empty.

isNonEmpty('hello'.trim()) // => true
isNonEmpty('   '.trim())   // => false
isNonEmpty(''.trim())      // => false

View source on GitHub