{"record":{"id":"2d793c5eaeb90ee0","repo":"TheAlgorithms/JavaScript","slug":"the-first-param-should-be-a-string","errorCode":null,"errorMessage":"The first param should be a string","messagePattern":"The first param should be a string","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/CheckWordOccurrence.js","lineNumber":10,"sourceCode":"/**\n * @function checkWordOccurrence\n * @description - this function count all the words in a sentence and return an word occurrence object\n * @param {string} str\n * @param {boolean} isCaseSensitive\n * @returns {Object}\n */\nconst checkWordOccurrence = (str, isCaseSensitive = false) => {\n  if (typeof str !== 'string') {\n    throw new TypeError('The first param should be a string')\n  }\n\n  if (typeof isCaseSensitive !== 'boolean') {\n    throw new TypeError('The second param should be a boolean')\n  }\n\n  const modifiedStr = isCaseSensitive ? str.toLowerCase() : str\n\n  return modifiedStr\n    .split(/\\s+/) // remove all spaces and distribute all word in List\n    .reduce((occurrence, word) => {\n      occurrence[word] = occurrence[word] + 1 || 1\n      return occurrence\n    }, {})\n}\n\nexport { checkWordOccurrence }\n","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/CheckWordOccurrence.js#L1-L28","documentation":"First-parameter guard in checkWordOccurrence. The function splits a sentence into words and counts occurrences. Before doing so, it requires the first argument (str) to be a string; anything else throws TypeError. This protects the later .split() and .reduce() calls.","triggerScenarios":"Calling checkWordOccurrence(null), checkWordOccurrence(123), checkWordOccurrence(), or checkWordOccurrence({text:'a b'}). Any first arg where typeof !== 'string'.","commonSituations":"Forgotten argument when refactoring a call site; reading textarea value that was never set; a payload field that is null instead of an empty string; an array passed where a joined string was expected.","solutions":["Pass a string sentence; if the source may be absent, coalesce to ''.","Validate the first argument's type at the call site before invoking.","If accepting arrays, join first: arr.join(' ')."],"exampleFix":"// before\ncheckWordOccurrence(maybeSentence)\n\n// after\ncheckWordOccurrence(typeof maybeSentence === 'string' ? maybeSentence : '')","handlingStrategy":"type-guard","validationCode":"if (typeof str !== 'string') {\n  throw new TypeError('str must be a string')\n}\ncheckWordOccurrence(str)","typeGuard":"const isString = (v) => typeof v === 'string'","tryCatchPattern":"try {\n  checkWordOccurrence(sentence)\n} catch (e) {\n  if (e instanceof TypeError && /first param/i.test(e.message)) { /* handle */ } else throw e\n}","preventionTips":["Coalesce nullable inputs: str ?? ''.","Join arrays before passing.","Keep a unit test that passes null to confirm your guard."],"tags":["string","validation","type-guard","word-count"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}