{"record":{"id":"a9b1fce3a2ddfc53","repo":"TheAlgorithms/JavaScript","slug":"argument-should-be-string-a9b1fc","errorCode":null,"errorMessage":"Argument should be string","messagePattern":"Argument should be string","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/FirstUniqueCharacter.js","lineNumber":13,"sourceCode":"/**\n * @function firstUniqChar\n * @description Given a string str, find the first non-repeating character in it and return its index. If it does not exist, return -1.\n * @param {String} str - The input string\n * @return {Number} - The index of first unique character.\n * @example firstUniqChar(\"javascript\") => 0\n * @example firstUniqChar(\"sesquipedalian\") => 3\n * @example firstUniqChar(\"aabb\") => -1\n */\n\nconst firstUniqChar = (str) => {\n  if (typeof str !== 'string') {\n    throw new TypeError('Argument should be string')\n  }\n  const count = new Map()\n\n  for (const char of str) {\n    if (!count[char]) {\n      count[char] = 1\n    } else {\n      count[char]++\n    }\n  }\n  for (let i = 0; i < str.length; i++) {\n    if (count[str[i]] === 1) return i\n  }\n  return -1\n}\n\nexport { firstUniqChar }\n","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/FirstUniqueCharacter.js#L1-L31","documentation":"Guard in firstUniqChar. The function finds the index of the first non-repeating character and requires the input to be a string, throwing TypeError otherwise. (Note: the body misuses a Map with bracket access, but the guard itself is a clean type check.)","triggerScenarios":"Calling firstUniqChar(undefined), firstUniqChar(null), firstUniqChar(0), firstUniqChar([]). Any input where typeof !== 'string'.","commonSituations":"A LeetCode-style input that arrives as a number when a string was expected; a payload field that is null on miss; a destructured value that does not exist.","solutions":["Pass a string; coerce numbers with String(n) or n.toString() first.","Default missing values to '' (returns -1).","Validate typeof at the boundary."],"exampleFix":"// before\nfirstUniqChar(maybeStr)\n\n// after\nfirstUniqChar(typeof maybeStr === 'string' ? maybeStr : String(maybeStr ?? ''))","handlingStrategy":"type-guard","validationCode":"if (typeof str !== 'string') {\n  throw new TypeError('str must be a string')\n}\nfirstUniqChar(str)","typeGuard":"const isString = (v) => typeof v === 'string'","tryCatchPattern":"try {\n  firstUniqChar(input)\n} catch (e) {\n  if (e instanceof TypeError) { /* not a string */ } else throw e\n}","preventionTips":["Coerce numbers with String() before passing.","Default missing values to '' (returns -1).","Validate typeof at the boundary."],"tags":["string","validation","type-guard","unique-character"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}