{"record":{"id":"7ff289b7dd94037a","repo":"TheAlgorithms/JavaScript","slug":"the-second-param-should-be-a-boolean","errorCode":null,"errorMessage":"The second param should be a boolean","messagePattern":"The second param should be a boolean","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/CheckWordOccurrence.js","lineNumber":14,"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":"Second-parameter guard in checkWordOccurrence. The optional isCaseSensitive parameter defaults to false, but if a caller passes it explicitly and it is not a boolean, the function throws TypeError. Note: because the default only applies when the argument is omitted, passing undefined explicitly still triggers the default and does NOT throw — but passing 'true' (string), 1, 0, or null does throw. Also note the downstream logic is inverted (isCaseSensitive lowercases the string), but the error itself is purely the type check.","triggerScenarios":"Calling checkWordOccurrence('a b a', 'true'), checkWordOccurrence('a b a', 1), checkWordOccurrence('a b a', null), or checkWordOccurrence('a b a', 'yes'). The second arg is present and typeof !== 'boolean'.","commonSituations":"Reading a flag from a config file or query string where booleans arrive as strings ('true'/'false'); deserialized JSON that used 0/1 for booleans; spreading an object whose flag field is undefined-null.","solutions":["Pass an actual boolean literal or convert first: flag === true or String(flag) === 'true'.","Omit the second argument entirely to accept the default (false).","Normalize at the boundary: const sens = String(raw).toLowerCase() === 'true'; then call with sens."],"exampleFix":"// before\ncheckWordOccurrence(text, rawFlag)\n\n// after\ncheckWordOccurrence(text, rawFlag === true || String(rawFlag).toLowerCase() === 'true')","handlingStrategy":"validation","validationCode":"const isCaseSensitive =\n  rawFlag === true || String(rawFlag).toLowerCase() === 'true'\ncheckWordOccurrence(str, isCaseSensitive)","typeGuard":"const isBoolean = (v) => typeof v === 'boolean'","tryCatchPattern":"try {\n  checkWordOccurrence(str, flag)\n} catch (e) {\n  if (e instanceof TypeError && /second param/i.test(e.message)) { /* coerce and retry */ } else throw e\n}","preventionTips":["Normalize boolean-ish config at the boundary.","Prefer omitting the arg to relying on the default.","Remember the default only applies when the arg is omitted/undefined."],"tags":["string","validation","type-guard","boolean","word-count"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}