TheAlgorithms/JavaScript · error · TypeError
Argument is not a string.
Error message
Argument is not a string.
What it means
Pre-condition guard at the top of checkSnakeCase. The function tests a name against a snake_case-ish regex ((.*?)_([a-zA-Z])*) and first asserts the input is a string. Any non-string input is rejected with TypeError before regex.test() runs. Note the regex itself is permissive (it matches almost any string containing an underscore), but the guard is strictly about type.
Source
Thrown at String/CheckSnakeCase.js:13
// CheckSnakeCase method checks the given string is in snake_case or not.
// Problem Source & Explanation: https://en.wikipedia.org/wiki/Naming_convention_(programming)
/**
* checkSnakeCase method returns true if the string in snake_case, else return the false.
* @param {String} varName the name of the variable to check.
* @returns `Boolean` return true if the string is in snake_case, else return false.
*/
const checkSnakeCase = (varName) => {
// firstly, check that input is a string or not.
if (typeof varName !== 'string') {
throw new TypeError('Argument is not a string.')
}
const pat = /(.*?)_([a-zA-Z])*/
return pat.test(varName)
}
export { checkSnakeCase }
View on GitHub (pinned to 5c39e87a9a)
Solutions
- Guarantee a string before calling: String(value) only after confirming it is not null/undefined, or check typeof.
- Provide a fallback default for the source variable.
- Add a runtime type check at the boundary where untrusted data enters your module.
Example fix
// before checkSnakeCase(process.env.VAR_NAME) // after const name = process.env.VAR_NAME if (typeof name === 'string') checkSnakeCase(name)
Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof varName !== 'string') {
throw new TypeError('varName must be a string')
}
checkSnakeCase(varName) Type guard
const isString = (v) => typeof v === 'string'
Try / catch
try {
checkSnakeCase(name)
} catch (e) {
if (e instanceof TypeError) { /* type error */ } else throw e
} Prevention
- Default env vars and config reads to ''.
- Validate at the trust boundary, not deep inside call chains.
- Use a shared isString helper across the module.
When it happens
Trigger: Calling checkSnakeCase(undefined), checkSnakeCase(null), checkSnakeCase(0), checkSnakeCase(Symbol('x')), or any value whose typeof !== 'string'.
Common situations: A field read from a config object that was not set; a value coming from URL search params that was parsed to a number; an env var that is undefined when the variable is missing from the environment.
Related errors
- Argument is not a string.
- The first param should be a string
- The second param should be a boolean
- Input should be a string
- Argument should be string
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/2604fefd9a11ac45.
Report an issue: GitHub.