TheAlgorithms/JavaScript · error · TypeError
Argument is not a string type
Error message
Argument is not a string type
What it means
Thrown by hexToBinary when hexString is not of type 'string'. This is the first guard in the function and runs before the hex-validity regex. It is a TypeError (not a plain Error) because the contract is about the argument type, not its content.
Source
Thrown at Conversions/HexToBinary.js:23
2: '0010',
3: '0011',
4: '0100',
5: '0101',
6: '0110',
7: '0111',
8: '1000',
9: '1001',
a: '1010',
b: '1011',
c: '1100',
d: '1101',
e: '1110',
f: '1111'
})[key.toLowerCase()] // select the binary number by valid hex key with the help javascript object
const hexToBinary = (hexString) => {
if (typeof hexString !== 'string') {
throw new TypeError('Argument is not a string type')
}
if (/[^\da-f]/gi.test(hexString)) {
throw new Error('Argument is not a valid HEX code!')
}
/*
Function for converting Hex to Binary
1. We convert every hexadecimal bit to 4 binary bits
2. Conversion goes by searching in the lookup table
*/
return hexString.replace(/[0-9a-f]/gi, (lexeme) => binLookup(lexeme))
}
export default hexToBinary
View on GitHub (pinned to 5c39e87a9a)
Solutions
- Coerce to string with String(...) only if the value is genuinely hex text, e.g. String(hexValue).
- If you have a Buffer/Uint8Array, convert with buf.toString('hex') first.
- Add a typeof hexString === 'string' guard in the caller and surface a clearer error.
Example fix
// before
hexToBinary(someBuffer)
// after
hexToBinary(someBuffer.toString('hex')) Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof hexString !== 'string') {
throw new TypeError('hexString must be a string')
}
hexToBinary(hexString) Type guard
const isString = (x) => typeof x === 'string'
Try / catch
try {
hexToBinary(hexString)
} catch (e) {
if (e instanceof TypeError && /not a string type/.test(e.message)) {
return hexToBinary(String(hexString))
}
throw e
} Prevention
- Convert Buffers/Uint8Arrays with .toString('hex') before calling.
- Coerce numeric hex values with String(...) at the boundary.
- Default optional parameters to '' rather than undefined.
When it happens
Trigger: Calling hexToBinary(255), hexToBinary(null), hexToBinary(['a','b']), or hexToBinary(Buffer.from('ab','hex')) — none of these are strings.
Common situations: Passing a Node.js Buffer or Uint8Array thinking it is a string; feeding a parsed JSON number; forgetting to coerce a DOM input value; defaulting the argument to undefined.
Related errors
- Only string arguments are allowed
- Argument is not a string.
- argument is not a Number
- Argument str should be String
- Argument should be string
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/63b66b150b37165c.
Report an issue: GitHub.