TheAlgorithms/JavaScript · error · TypeError
Invalid Input Type
Error message
Invalid Input Type
What it means
Guard in lower. The function lowercases a string by replacing [A-Z] with charCode+32 equivalents and requires the input to be a string, throwing TypeError otherwise. This protects .replace() from non-string values.
Source
Thrown at String/Lower.js:12
/**
* @function lower
* @description Will convert the entire string to lowercase letters.
* @param {String} str - The input string
* @returns {String} Lowercase string
* @example lower("HELLO") => hello
* @example lower("He_llo") => he_llo
*/
const lower = (str) => {
if (typeof str !== 'string') {
throw new TypeError('Invalid Input Type')
}
return str.replace(/[A-Z]/g, (char) =>
String.fromCharCode(char.charCodeAt() + 32)
)
}
export default lower
View on GitHub (pinned to 5c39e87a9a)
Solutions
- Pass a string; default missing values to ''.
- Validate typeof at the call site.
- Consider the native str.toLowerCase() for full Unicode coverage.
Example fix
// before lower(maybeStr) // after lower(typeof maybeStr === 'string' ? maybeStr : '')
Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof str !== 'string') {
throw new TypeError('str must be a string')
}
lower(str) Type guard
const isString = (v) => typeof v === 'string'
Try / catch
try {
lower(input)
} catch (e) {
if (e instanceof TypeError && /Invalid Input Type/i.test(e.message)) { /* not a string */ } else throw e
} Prevention
- Default optional fields to ''.
- For Unicode correctness prefer native str.toLowerCase().
- Validate typeof at the boundary.
When it happens
Trigger: Calling lower(undefined), lower(null), lower(42), lower(['A']). Any input where typeof !== 'string'.
Common situations: Optional field omitted; value coerced to a number earlier; array passed instead of a string. Note: for production use String.prototype.toLowerCase() is more robust (handles Unicode), so calling this custom lower is itself a choice to flag.
Related errors
- Argument is not a string.
- Argument is not a string.
- The first param should be a string
- The second param should be a boolean
- Input should be a string
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/f94dcf009fd45703.
Report an issue: GitHub.