{"record":{"id":"0da2bd8620e7c4cb","repo":"TheAlgorithms/JavaScript","slug":"the-given-value-is-not-a-string-0da2bd","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/ReverseWords.js","lineNumber":8,"sourceCode":"/**\n * @function reverseWords\n * @param {string} str\n * @returns {string} - reverse string\n */\nconst reverseWords = (str) => {\n  if (typeof str !== 'string') {\n    throw new TypeError('The given value is not a string')\n  }\n\n  return str\n    .split(/\\s+/) // create an array with each word in string\n    .reduceRight((reverseStr, word) => `${reverseStr} ${word}`, '') // traverse the array from last & create an string\n    .trim() // remove the first useless space\n}\n\nexport default reverseWords\n","sourceCodeStart":1,"sourceCodeEnd":18,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/ReverseWords.js#L1-L18","documentation":"Thrown by reverseWords() in String/ReverseWords.js when typeof str !== 'string'. A TypeError raised before the .split(/\\s+/) pipeline runs, because split only exists meaningfully on strings and the reduceRight builds a new string. Only the type is checked, so an empty string '' is accepted and returns '' after trim.","triggerScenarios":"Calling reverseWords(42), reverseWords(null), reverseWords(undefined), reverseWords(['hello world']), reverseWords({ text: 'hi' }). Empty string reverseWords('') does NOT throw — it returns ''.","commonSituations":"Reading req.body fields that arrived as a number from JSON; passing a Node Buffer; forgetting to .toString() a value read from a stream; mixing up argument order so an options object lands in str.","solutions":["Pass a string primitive: reverseWords('hello world').","Coerce at the boundary: reverseWords(String(value)).","Verify the upstream data shape — if value is sometimes non-string, fix the producer (e.g. schema-validate request bodies).","If you intentionally store text in an object, read the right property before passing it."],"exampleFix":"// before\nconst r = reverseWords(req.body.title) // title is sometimes a number in malformed payloads\n\n// after\nconst r = reverseWords(typeof req.body.title === 'string' ? req.body.title : String(req.body.title ?? ''))","handlingStrategy":"type-guard","validationCode":"function reverseWordsSafe(v) {\n  if (typeof v !== 'string') v = String(v ?? '')\n  return reverseWords(v)\n}","typeGuard":"const isString = (v) => typeof v === 'string'","tryCatchPattern":"try { reverseWords(s) } catch (e) { if (e instanceof TypeError && /not a string/.test(e.message)) s = String(s); else throw e }","preventionTips":["Coerce all incoming form/JSON fields to strings at the boundary.","Read .value from DOM inputs, never the element itself.","Validate request bodies with a schema that declares string fields."],"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"}