Skip to content

isHexString

Checks whether a value is a hexadecimal string. A value is considered a hex string when it is a non-empty string containing only hexadecimal digits (0-9, a-f, A-F).

This helper does not accept prefixes such as 0x or #, and it does not allow whitespace, punctuation, or any non-hex characters.

It never throws and never mutates input. Use it when you need to confirm that a value already represents raw hexadecimal data before further processing or validation.

Signature

function isHexString(value: unknown): value is string;

Parameters

Name Data type Description
value unknown

Returns

  • true: if the value is a valid hex string, otherwise false.

Behavior

  • Requires one or more hexadecimal digits.
  • Accepts uppercase or lowercase hex characters.
  • Rejects prefixes such as 0x or #.
  • Rejects whitespace, punctuation, and non-hex characters.
  • Never mutates input.
  • Never throws.

Examples

isHexString("a")
// true

isHexString("0a1b2c3d")
// true

isHexString("ABCDEF")
// true

isHexString("0x1234")
// false (prefix)

isHexString("12 34")
// false (whitespace)

isHexString("not-hex")
// false

Notes

  • This helper does not coerce values. Non-strings always return false.
  • Only raw hexadecimal content is accepted; prefixed formats must be validated separately.
  • Suitable for validating binary encodings, identifiers, checksums, and any context requiring strict hexadecimal input.
  • The helper never throws and never mutates input.