TheAlgorithms/JavaScript · error · TypeError
Given input is not a string
Error message
Given input is not a string
What it means
Guard in checkIfPatternExists. The function performs naive pattern matching and requires BOTH text and pattern to be strings, throwing TypeError if either fails typeof. The single shared message does not indicate which argument was invalid.
Source
Thrown at String/PatternMatching.js:12
/*
Pattern matching is case insensitive as
the inputs are converted to lower case before the
algorithm is run.
The algorithm will run through the entire text and
return the starting index if the given pattern is
available in the text
*/
const checkIfPatternExists = (text, pattern) => {
if (typeof text !== 'string' || typeof pattern !== 'string') {
throw new TypeError('Given input is not a string')
}
const textLength = text.length // Store the length of the text in a variable
const patternLength = pattern.length // Store the length of the pattern in a variable
// Iterate through the text until the textlength - patternlength index
for (let i = 0; i <= textLength - patternLength; i++) {
// For each character in the text check if the subsequent character
// are matching the given pattern; if not break from the condition
for (let j = 0; j < textLength; j++) {
if (text[i + j] !== pattern[j]) break
// For each iteration of j check if the value of
// j + 1 is equal to the length of the pattern
if (j + 1 === patternLength) {
return `Given pattern is found at index ${i}`
}
}
}View on GitHub (pinned to 5c39e87a9a)
Solutions
- Ensure both arguments are strings; default the pattern when optional.
- If you need regex matching, use RegExp.test/String.match instead of this string-only utility.
- Pre-validate both types at the call site.
Example fix
// before
checkIfPatternExists(haystack, needle)
// after
if (typeof haystack === 'string' && typeof needle === 'string') {
checkIfPatternExists(haystack, needle)
} Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof text !== 'string' || typeof pattern !== 'string') {
throw new TypeError('text and pattern must be strings')
}
checkIfPatternExists(text, pattern) Type guard
const areStrings = (a, b) => typeof a === 'string' && typeof b === 'string'
Try / catch
try {
checkIfPatternExists(haystack, needle)
} catch (e) {
if (e instanceof TypeError) { /* one arg was not a string */ } else throw e
} Prevention
- Default the pattern when it is optional.
- For regex matching use RegExp.test directly.
- The shared message is ambiguous — log both args on failure.
When it happens
Trigger: Calling checkIfPatternExists(null, 'ab'), checkIfPatternExists('text', undefined), checkIfPatternExists(123, '1'), checkIfPatternExists('text', /ab/). Either argument non-string.
Common situations: An optional pattern parameter omitted (becomes undefined); a regex passed where a string was expected; a haystack read from a source that returned null.
Related errors
- Argument should be string
- Argument is not a string.
- Argument is not a string.
- The first param should be a string
- The second param should be a boolean
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/de8fbbd1688938e3.
Report an issue: GitHub.