TheAlgorithms/JavaScript · error · TypeError
Both arguments should be strings.
Error message
Both arguments should be strings.
What it means
Thrown as a TypeError by checkAnagramRegex() when either str1 or str2 is not a string. The function spreads str1 ([...str1]) and compares lengths/characters, all of which assume string inputs. Both arguments must be primitive strings; missing or non-string either one triggers the error.
Source
Thrown at String/CheckAnagram.js:14
// An [Anagram](https://en.wikipedia.org/wiki/Anagram) is a string that is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Anagram check is not case-sensitive;
/**
* @function checkAnagramRegex
* @param {string} str1
* @param {string} str2
* @returns {boolean}
* @description - check anagram with the help of Regex
* @example - checkAnagramRegex('node', 'deno') => true
* @example - checkAnagramRegex('Eleven plus two', 'Twelve plus one') => true
*/
const checkAnagramRegex = (str1, str2) => {
// check that inputs are strings.
if (typeof str1 !== 'string' || typeof str2 !== 'string') {
throw new TypeError('Both arguments should be strings.')
}
// If both strings have not same lengths then they can not be anagram.
if (str1.length !== str2.length) {
return false
}
/**
* str1 converted to an array and traverse each letter of str1 by reduce method
* reduce method return string which is empty or not.
*/
return ![...str1].reduce(
(str2Acc, cur) => str2Acc.replace(new RegExp(cur, 'i'), ''), // remove the similar letter from str2Acc in case-insensitive
str2
)
}
/**View on GitHub (pinned to 5c39e87a9a)
Solutions
- Default both parameters: checkAnagramRegex(str1 ?? '', str2 ?? '').
- Guard both: if (typeof a === 'string' && typeof b === 'string').
- Coerce inputs: checkAnagramRegex(String(a), String(b)).
Example fix
// before checkAnagramRegex(input1, input2) // after checkAnagramRegex(String(input1 ?? ''), String(input2 ?? ''))
Defensive patterns
Strategy: type-guard
Validate before calling
function safeCheckAnagramRegex(a, b) {
if (typeof a !== 'string' || typeof b !== 'string') {
throw new TypeError('Both arguments must be strings')
}
return checkAnagramRegex(a, b)
} Type guard
function areBothStrings(a, b) {
return typeof a === 'string' && typeof b === 'string'
} Try / catch
try {
checkAnagramRegex(a, b)
} catch (e) {
if (e instanceof TypeError && e.message.includes('Both arguments should be strings')) {
return checkAnagramRegex(String(a ?? ''), String(b ?? ''))
}
throw e
} Prevention
- Default both parameters: (a = '', b = '').
- Coerce both inputs with String() before calling.
- Validate types where data enters the system.
When it happens
Trigger: Calling checkAnagramRegex(null, 'node'), checkAnagramRegex('node', undefined), checkAnagramRegex(123, '123'), or checkAnagramRegex(['n','o'], 'no'). Only one bad argument is enough since the condition uses OR.
Common situations: Optional function parameters not defaulted; one side coming from JSON that omits a field; comparing a value to itself where the value is undefined.
Related errors
- Invalid Input
- Argument should be string
- Argument is not a string.
- Argument is not a string
- Argument is not a string.
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/29e176ff87cea150.
Report an issue: GitHub.