{"record":{"id":"29e176ff87cea150","repo":"TheAlgorithms/JavaScript","slug":"both-arguments-should-be-strings","errorCode":null,"errorMessage":"Both arguments should be strings.","messagePattern":"Both arguments should be strings\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/CheckAnagram.js","lineNumber":14,"sourceCode":"// An [Anagram](https://en.wikipedia.org/wiki/Anagram) is a string that is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Anagram check is not case-sensitive;\n/**\n * @function checkAnagramRegex\n * @param {string} str1\n * @param {string} str2\n * @returns {boolean}\n * @description - check anagram with the help of Regex\n * @example - checkAnagramRegex('node', 'deno') => true\n * @example - checkAnagramRegex('Eleven plus two', 'Twelve plus one') => true\n */\nconst checkAnagramRegex = (str1, str2) => {\n  // check that inputs are strings.\n  if (typeof str1 !== 'string' || typeof str2 !== 'string') {\n    throw new TypeError('Both arguments should be strings.')\n  }\n\n  // If both strings have not same lengths then they can not be anagram.\n  if (str1.length !== str2.length) {\n    return false\n  }\n\n  /**\n   * str1 converted to an array and traverse each letter of str1 by reduce method\n   * reduce method return string which is empty or not.\n   */\n  return ![...str1].reduce(\n    (str2Acc, cur) => str2Acc.replace(new RegExp(cur, 'i'), ''), // remove the similar letter from str2Acc in case-insensitive\n    str2\n  )\n}\n\n/**","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/CheckAnagram.js#L1-L32","documentation":"Thrown as a TypeError by checkAnagramRegex() when either str1 or str2 is not a string. The function spreads str1 ([...str1]) and compares lengths/characters, all of which assume string inputs. Both arguments must be primitive strings; missing or non-string either one triggers the error.","triggerScenarios":"Calling checkAnagramRegex(null, 'node'), checkAnagramRegex('node', undefined), checkAnagramRegex(123, '123'), or checkAnagramRegex(['n','o'], 'no'). Only one bad argument is enough since the condition uses OR.","commonSituations":"Optional function parameters not defaulted; one side coming from JSON that omits a field; comparing a value to itself where the value is undefined.","solutions":["Default both parameters: checkAnagramRegex(str1 ?? '', str2 ?? '').","Guard both: if (typeof a === 'string' && typeof b === 'string').","Coerce inputs: checkAnagramRegex(String(a), String(b))."],"exampleFix":"// before\ncheckAnagramRegex(input1, input2)\n\n// after\ncheckAnagramRegex(String(input1 ?? ''), String(input2 ?? ''))","handlingStrategy":"type-guard","validationCode":"function safeCheckAnagramRegex(a, b) {\n  if (typeof a !== 'string' || typeof b !== 'string') {\n    throw new TypeError('Both arguments must be strings')\n  }\n  return checkAnagramRegex(a, b)\n}","typeGuard":"function areBothStrings(a, b) {\n  return typeof a === 'string' && typeof b === 'string'\n}","tryCatchPattern":"try {\n  checkAnagramRegex(a, b)\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes('Both arguments should be strings')) {\n    return checkAnagramRegex(String(a ?? ''), String(b ?? ''))\n  }\n  throw e\n}","preventionTips":["Default both parameters: (a = '', b = '').","Coerce both inputs with String() before calling.","Validate types where data enters the system."],"tags":["type-check","string","anagram","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}