TheAlgorithms/JavaScript · error · TypeError
The second param should be a boolean
Error message
The second param should be a boolean
What it means
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.
Source
Thrown at String/CheckWordOccurrence.js:14
/**
* @function checkWordOccurrence
* @description - this function count all the words in a sentence and return an word occurrence object
* @param {string} str
* @param {boolean} isCaseSensitive
* @returns {Object}
*/
const checkWordOccurrence = (str, isCaseSensitive = false) => {
if (typeof str !== 'string') {
throw new TypeError('The first param should be a string')
}
if (typeof isCaseSensitive !== 'boolean') {
throw new TypeError('The second param should be a boolean')
}
const modifiedStr = isCaseSensitive ? str.toLowerCase() : str
return modifiedStr
.split(/\s+/) // remove all spaces and distribute all word in List
.reduce((occurrence, word) => {
occurrence[word] = occurrence[word] + 1 || 1
return occurrence
}, {})
}
export { checkWordOccurrence }
View on GitHub (pinned to 5c39e87a9a)
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.
Example fix
// before checkWordOccurrence(text, rawFlag) // after checkWordOccurrence(text, rawFlag === true || String(rawFlag).toLowerCase() === 'true')
Defensive patterns
Strategy: validation
Validate before calling
const isCaseSensitive = rawFlag === true || String(rawFlag).toLowerCase() === 'true' checkWordOccurrence(str, isCaseSensitive)
Type guard
const isBoolean = (v) => typeof v === 'boolean'
Try / catch
try {
checkWordOccurrence(str, flag)
} catch (e) {
if (e instanceof TypeError && /second param/i.test(e.message)) { /* coerce and retry */ } else throw e
} Prevention
- 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.
When it happens
Trigger: 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'.
Common situations: 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.
Related errors
- The first param should be a string
- the param should be string
- Argument is not a string.
- Argument is not a string.
- Input should be a string
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/7ff289b7dd94037a.
Report an issue: GitHub.