{"record":{"id":"9cb25a4ef969e97d","repo":"TheAlgorithms/JavaScript","slug":"argument-should-be-string-9cb25a","errorCode":null,"errorMessage":"Argument should be string","messagePattern":"Argument should be string","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/AlphaNumericPalindrome.js","lineNumber":19,"sourceCode":"/**\n * @function alphaNumericPalindrome\n * @description alphaNumericPalindrome should return true if the string has alphanumeric characters that are palindrome irrespective of special characters and the letter case.\n * @param {string} str the string to check\n * @returns {boolean}\n * @see [Palindrome](https://en.wikipedia.org/wiki/Palindrome)\n * @example\n * The function alphaNumericPalindrome() receives a string with varying formats\n * like \"racecar\", \"RaceCar\", and \"race CAR\"\n * The string can also have special characters\n * like \"2A3*3a2\", \"2A3 3a2\", and \"2_A3*3#A2\"\n *\n * But the catch is, we have to check only if the alphanumeric characters\n * are palindrome i.e remove spaces, symbols, punctuation etc\n * and the case of the characters doesn't matter\n */\nconst alphaNumericPalindrome = (str) => {\n  if (typeof str !== 'string') {\n    throw new TypeError('Argument should be string')\n  }\n\n  // removing all the special characters and turning everything to lowercase\n  const newStr = str.replace(/[^a-z0-9]+/gi, '').toLowerCase()\n  const midIndex = newStr.length >> 1 // x >> y = floor(x / 2^y)\n\n  for (let i = 0; i < midIndex; i++) {\n    if (newStr.at(i) !== newStr.at(~i)) {\n      // ~n = -(n + 1)\n      return false\n    }\n  }\n\n  return true\n}\n\nexport default alphaNumericPalindrome\n","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/AlphaNumericPalindrome.js#L1-L37","documentation":"Thrown as a TypeError by alphaNumericPalindrome() when the input is not a string. The function uses str.replace() with a regex and String.prototype.at(), both of which require a string receiver. The check is strict (typeof === 'string'), so numbers, String objects (new String()), and arrays are all rejected.","triggerScenarios":"Calling alphaNumericPalindrome(12321), alphaNumericPalindrome(['r','a','c','e','c','a','r']), alphaNumericPalindrome(null), or alphaNumericPalindrome(new String('racecar')) — the last fails because typeof new String() === 'object'.","commonSituations":"Numeric IDs that look palindromic; String wrapper objects from older codebases; untyped form input parsed as a number.","solutions":["Coerce with String(): alphaNumericPalindrome(String(value)).","Avoid String wrapper objects; use string literals.","Type-guard at the boundary: if (typeof str === 'string')."],"exampleFix":"// before\nalphaNumericPalindrome(userId) // userId is a number\n\n// after\nalphaNumericPalindrome(String(userId))","handlingStrategy":"type-guard","validationCode":"function safeAlphaNumericPalindrome(value) {\n  if (typeof value !== 'string') {\n    throw new TypeError('Expected a string')\n  }\n  return alphaNumericPalindrome(value)\n}","typeGuard":"function isString(v) {\n  return typeof v === 'string'\n}","tryCatchPattern":"try {\n  alphaNumericPalindrome(input)\n} catch (e) {\n  if (e instanceof TypeError && e.message === 'Argument should be string') {\n    return alphaNumericPalindrome(String(input))\n  }\n  throw e\n}","preventionTips":["Coerce with String() at the boundary.","Avoid String wrapper objects (new String()).","Type the parameter as string in TypeScript."],"tags":["type-check","string","palindrome","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}