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 CheckPascalCase. The function tests a variable name against the regex /^[A-Z][A-Za-z]*$/ (an uppercase letter followed by zero or more letters). Before it can call regex.test(), it rejects any input whose typeof is not 'string' with TypeError. This is the library's standard pattern for protecting .test() from non-string arguments.
Source
Thrown at String/CheckPascalCase.js:13
// CheckPascalCase method checks the given string is in PascalCase or not.
// Problem Source & Explanation: https://www.theserverside.com/definition/Pascal-case
/**
* CheckPascalCase method returns true if the string in PascalCase, else return the false.
* @param {String} VarName the name of the variable to check.
* @returns `Boolean` return true if the string is in PascalCase, else return false.
*/
const CheckPascalCase = (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-Z][A-Za-z]*$/
return pat.test(VarName)
}
export { CheckPascalCase }
View on GitHub (pinned to 5c39e87a9a)
Solutions
- Pass only string values: coerce or validate the source before calling, e.g. ensure the value is a string via typeof.
- Default the variable when it may be missing: const name = raw?.name ?? ''.
- Wrap the call in try/catch when the input crosses a trust boundary (user input, external API payload).
Example fix
// before CheckPascalCase(maybeName) // after if (typeof maybeName === 'string') CheckPascalCase(maybeName) // or CheckPascalCase(typeof maybeName === 'string' ? maybeName : '')
Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof VarName !== 'string') {
throw new TypeError('VarName must be a string, got ' + typeof VarName)
}
CheckPascalCase(VarName) Type guard
const isNonEmptyString = (v) => typeof v === 'string' && v.length > 0
Try / catch
try {
CheckPascalCase(name)
} catch (e) {
if (e instanceof TypeError && /not a string/i.test(e.message)) {
// handle bad input type
} else throw e
} Prevention
- Type all boundary inputs in JSDoc/@ts-check or TypeScript.
- Coerce optional fields with ?? '' at the read site.
- Never pass raw parsed-JSON fields without a typeof check.
When it happens
Trigger: Calling CheckPascalCase(undefined), CheckPascalCase(null), CheckPascalCase(42), CheckPascalCase({name:'Foo'}), or CheckPascalCase(['Foo']). Any value where typeof !== 'string'.
Common situations: Reading an optional field from JSON.parse where the key is missing (yields undefined); form/CLI input parsed as a number; a destructured object property that was renamed or absent; spreading an array element that is undefined.
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/ca476222f41b5c2e.
Report an issue: GitHub.