{"record":{"id":"de8fbbd1688938e3","repo":"TheAlgorithms/JavaScript","slug":"given-input-is-not-a-string","errorCode":null,"errorMessage":"Given input is not a string","messagePattern":"Given input is not a string","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/PatternMatching.js","lineNumber":12,"sourceCode":"/*\nPattern matching is case insensitive as\nthe inputs are converted to lower case before the\nalgorithm is run.\n\nThe algorithm will run through the entire text and\nreturn the starting index if the given pattern is\navailable in the text\n*/\nconst checkIfPatternExists = (text, pattern) => {\n  if (typeof text !== 'string' || typeof pattern !== 'string') {\n    throw new TypeError('Given input is not a string')\n  }\n  const textLength = text.length // Store the length of the text in a variable\n  const patternLength = pattern.length // Store the length of the pattern in a variable\n\n  // Iterate through the text until the textlength - patternlength index\n  for (let i = 0; i <= textLength - patternLength; i++) {\n    // For each character in the text check if the subsequent character\n    // are matching the given pattern; if not break from the condition\n    for (let j = 0; j < textLength; j++) {\n      if (text[i + j] !== pattern[j]) break\n\n      // For each iteration of j check if the value of\n      // j + 1 is equal to the length of the pattern\n      if (j + 1 === patternLength) {\n        return `Given pattern is found at index ${i}`\n      }\n    }\n  }","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/PatternMatching.js#L1-L30","documentation":"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.","triggerScenarios":"Calling checkIfPatternExists(null, 'ab'), checkIfPatternExists('text', undefined), checkIfPatternExists(123, '1'), checkIfPatternExists('text', /ab/). Either argument non-string.","commonSituations":"An optional pattern parameter omitted (becomes undefined); a regex passed where a string was expected; a haystack read from a source that returned null.","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."],"exampleFix":"// before\ncheckIfPatternExists(haystack, needle)\n\n// after\nif (typeof haystack === 'string' && typeof needle === 'string') {\n  checkIfPatternExists(haystack, needle)\n}","handlingStrategy":"type-guard","validationCode":"if (typeof text !== 'string' || typeof pattern !== 'string') {\n  throw new TypeError('text and pattern must be strings')\n}\ncheckIfPatternExists(text, pattern)","typeGuard":"const areStrings = (a, b) => typeof a === 'string' && typeof b === 'string'","tryCatchPattern":"try {\n  checkIfPatternExists(haystack, needle)\n} catch (e) {\n  if (e instanceof TypeError) { /* one arg was not a string */ } else throw e\n}","preventionTips":["Default the pattern when it is optional.","For regex matching use RegExp.test directly.","The shared message is ambiguous — log both args on failure."],"tags":["string","validation","type-guard","pattern-matching","ambiguous-message"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}