{"record":{"id":"c129bfc7ee663c56","repo":"TheAlgorithms/JavaScript","slug":"argument-should-be-string-c129bf","errorCode":null,"errorMessage":"Argument should be string","messagePattern":"Argument should be string","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/Upper.js","lineNumber":11,"sourceCode":"/**\n * @function upper\n * @description Will convert the entire string to uppercase letters.\n * @param {String} str - The input string\n * @return {String} Uppercase string\n * @example upper(\"hello\") => HELLO\n * @example upper(\"He_llo\") => HE_LLO\n */\nconst upper = (str) => {\n  if (typeof str !== 'string') {\n    throw new TypeError('Argument should be string')\n  }\n\n  return str.replace(/[a-z]/g, (char) =>\n    String.fromCharCode(char.charCodeAt() - 32)\n  )\n}\n\nexport default upper\n","sourceCodeStart":1,"sourceCodeEnd":20,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/Upper.js#L1-L20","documentation":"Thrown by upper() in String/Upper.js when typeof str !== 'string'. A TypeError guarding the .replace(/[a-z]/g, ...) call which manually subtracts 32 from charCodeAt to shift lowercase ASCII to uppercase. Non-string input would either lack .replace or produce wrong results, so the type is enforced up front.","triggerScenarios":"Calling upper(123), upper(null), upper(undefined), upper(['abc']), upper({0:'a',length:1}), upper(Symbol('x')). Empty string upper('') does NOT throw — returns ''.","commonSituations":"Forwarding a number from a numeric form field without String(); passing a value from a Map/WeakMap lookup that returned undefined; a variable shadowed by a reassignment to a non-string; treating Buffer contents as a string without .toString().","solutions":["Pass a primitive string: upper('hello').","Coerce non-string inputs: upper(String(value)).","Avoid the boxed String constructor (new String('x') is typeof 'object' and will throw).","Prefer the native str.toUpperCase() when you do not specifically need this implementation."],"exampleFix":"// before\nconst up = upper(input) // input is sometimes a number from a form field\n\n// after\nconst up = upper(typeof input === 'string' ? input : String(input))","handlingStrategy":"type-guard","validationCode":"function upperSafe(v) {\n  if (typeof v !== 'string') throw new TypeError('Argument should be string')\n  return upper(v)\n}","typeGuard":"const isString = (v) => typeof v === 'string'","tryCatchPattern":"try { upper(s) } catch (e) { if (e instanceof TypeError) s = String(s); else throw e }","preventionTips":["Use primitive strings, not boxed String objects.","Coerce numeric form fields with String() before processing.","Prefer native String.prototype.toUpperCase() unless you specifically need this implementation."],"tags":["string","type-check","validation","ascii"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}