TheAlgorithms/JavaScript · error · TypeError
Argument should be string
Error message
Argument should be string
What it means
Atbash rejects any non-string input because it calls str.replace(/[a-z]/gi, ...). Passing a non-string would either coerce oddly or throw a less informative error from String.prototype.replace, so the guard validates up front.
Source
Thrown at Ciphers/Atbash.js:10
/**
* @function Atbash - Decrypt a Atbash cipher
* @description - The Atbash cipher is a particular type of monoalphabetic cipher formed by taking the alphabet and mapping it to its reverse, so that the first letter becomes the last letter, the second letter becomes the second to last letter, and so on.
* @param {string} str - string to be decrypted/encrypt
* @return {string} decrypted/encrypted string
* @see - [wiki](https://en.wikipedia.org/wiki/Atbash)
*/
const Atbash = (str) => {
if (typeof str !== 'string') {
throw new TypeError('Argument should be string')
}
return str.replace(/[a-z]/gi, (char) => {
const charCode = char.charCodeAt()
if (/[A-Z]/.test(char)) {
return String.fromCharCode(90 + 65 - charCode)
}
return String.fromCharCode(122 + 97 - charCode)
})
}
export default Atbash
View on GitHub (pinned to 5c39e87a9a)
Solutions
- Pass a string: Atbash('secret').
- Convert with String(value) or .toString() for non-string inputs.
- Decode Buffers to utf8 before calling.
Example fix
// before Atbash(12345) // after Atbash(String(12345))
Defensive patterns
Strategy: type-guard
Validate before calling
function atbashSafe(v) {
return Atbash(typeof v === 'string' ? v : String(v));
} Type guard
/** @param {unknown} s @returns {s is string} */
const isString = s => typeof s === 'string'; Try / catch
try { return Atbash(text); }
catch (e) {
if (e instanceof TypeError && /should be string/.test(e.message)) {
return Atbash(String(text));
}
throw e;
} Prevention
- Wrap potentially non-string values with String().
- Decode Buffers to utf8 before ciphering.
- Validate typeof at the input boundary.
When it happens
Trigger: Passing a number, null, undefined, object, array, or Buffer instead of the text string.
Common situations: Encrypting a numeric ID without converting to string, or passing a Buffer from file/network input.
Related errors
- Argument str should be String
- Argument should be string
- Coefficient a, b should be number
- Arguments are invalid
- Arguments type are invalid
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/30705a464912d506.
Report an issue: GitHub.