{"record":{"id":"76be01f08bbefce7","repo":"TheAlgorithms/JavaScript","slug":"the-given-value-is-not-a-string-76be01","errorCode":null,"errorMessage":"The given value is not a string","messagePattern":"The given value is not a string","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/ReverseString.js","lineNumber":6,"sourceCode":"/**\n * A short example showing how to reverse a string.\n */\nfunction ReverseStringIterative(string) {\n  if (typeof string !== 'string') {\n    throw new TypeError('The given value is not a string')\n  }\n  let reversedString = ''\n  let index\n\n  for (index = string.length - 1; index >= 0; index--) {\n    reversedString += string[index]\n  }\n\n  return reversedString\n}\n\n/**\n *\n * @author dev-madhurendra\n * Reverses a number by converting it to a string.\n *\n * @param {string} str - The number to reverse.\n * @returns {string} The reversed number.","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/ReverseString.js#L1-L24","documentation":"Thrown by ReverseStringIterative() in String/ReverseString.js when the argument's typeof is not 'string'. This is a TypeError (not a generic Error), signalling a programming contract violation rather than bad data. The function then iterates the string by index, so it must have a string input to subscript.","triggerScenarios":"Calling ReverseStringIterative(42), ReverseStringIterative(null), ReverseStringIterative(undefined), ReverseStringIterative(['a','b']), ReverseStringIterative({}). Note: a String object (new String('x')) would also throw because typeof is 'object'.","commonSituations":"Forwarding DOM node values or event targets without reading .value/.textContent; passing a Buffer in Node; receiving parsed JSON where a field was numeric; using the boxed String constructor new String('hi') which yields typeof 'object'.","solutions":["Pass a primitive string: ReverseStringIterative('hello').","If the source may be non-string, coerce explicitly: ReverseStringIterative(String(value)).","Unbox boxed strings: if (value instanceof String) value = value.valueOf().","Read the right property from your source (e.g. event.target.value, not event.target)."],"exampleFix":"// before\nconst out = ReverseStringIterative(inputEl) // inputEl is an HTMLElement, not its value\n\n// after\nconst out = ReverseStringIterative(String(inputEl.value))","handlingStrategy":"type-guard","validationCode":"function reverseSafe(v) {\n  if (typeof v !== 'string') throw new TypeError('expected string')\n  return ReverseStringIterative(v)\n}","typeGuard":"const isString = (v) => typeof v === 'string'","tryCatchPattern":"try { ReverseStringIterative(input) }\ncatch (e) { if (e instanceof TypeError) { /* coerce and retry */ } else throw e }","preventionTips":["Never pass DOM elements or Buffers directly — read .value / call .toString().","Avoid new String('...'); use primitive strings.","Type-check at request boundaries before calling string utilities."],"tags":["string","type-check","validation","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}