{"record":{"id":"02a3de83f268018d","repo":"TheAlgorithms/JavaScript","slug":"invalid-input-02a3de","errorCode":null,"errorMessage":"Invalid Input","messagePattern":"Invalid Input","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Recursive/Palindrome.js","lineNumber":11,"sourceCode":"/**\n * @function Palindrome\n * @description Check whether the given string is Palindrome or not.\n * @param {String} str - The input string\n * @return {Boolean}.\n * @see [Palindrome](https://en.wikipedia.org/wiki/Palindrome)\n */\n\nconst palindrome = (str) => {\n  if (typeof str !== 'string') {\n    throw new TypeError('Invalid Input')\n  }\n\n  if (str.length <= 1) {\n    return true\n  }\n\n  if (str[0] !== str[str.length - 1]) {\n    return false\n  } else {\n    return palindrome(str.slice(1, str.length - 1))\n  }\n}\n\nexport { palindrome }\n","sourceCodeStart":1,"sourceCodeEnd":26,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Recursive/Palindrome.js#L1-L26","documentation":"Thrown as a TypeError by palindrome() when the input is not of type 'string'. The recursive implementation indexes into str with str[0], str[str.length-1], and str.slice(), all of which assume string semantics; passing a non-string would either behave incorrectly or throw a less descriptive error. The library uses a strict typeof check, so numbers, objects, and arrays are all rejected even if they 'look' palindromic.","triggerScenarios":"Calling palindrome(12321), palindrome(['a','b','a']), palindrome(null), palindrome(undefined), or palindrome({0:'a'}). Numbers are the most common offender since 12321 'looks' like a numeric palindrome.","commonSituations":"Receiving untyped user input from a form field parsed as a number; JSON payloads where a field is sometimes a number; passing a value through several functions that lost its string typing.","solutions":["Coerce the input to a string first: palindrome(String(value)).","Add a type guard at the boundary: if (typeof value === 'string') palindrome(value).","If using TypeScript, annotate the parameter as str: string so the call site is checked at compile time."],"exampleFix":"// before\nconst result = palindrome(userInput) // userInput may be a number\n\n// after\nconst result = palindrome(String(userInput))","handlingStrategy":"type-guard","validationCode":"function safePalindrome(value) {\n  if (typeof value !== 'string') {\n    throw new TypeError('Expected a string')\n  }\n  return palindrome(value)\n}","typeGuard":"function isString(v) {\n  return typeof v === 'string'\n}","tryCatchPattern":"try {\n  palindrome(input)\n} catch (e) {\n  if (e instanceof TypeError && e.message === 'Invalid Input') {\n    return palindrome(String(input))\n  }\n  throw e\n}","preventionTips":["Coerce unknown input with String() before calling.","In TypeScript, type the parameter as string to catch misuse at compile time.","Sanitize form/JSON input at the boundary, not at the algorithm."],"tags":["type-check","string","recursion","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}