TheAlgorithms/JavaScript · error · TypeError

Argument should be string

Error message

Argument should be string

What it means

Thrown as a TypeError by alphaNumericPalindrome() when the input is not a string. The function uses str.replace() with a regex and String.prototype.at(), both of which require a string receiver. The check is strict (typeof === 'string'), so numbers, String objects (new String()), and arrays are all rejected.

Source

Thrown at String/AlphaNumericPalindrome.js:19

/**
 * @function alphaNumericPalindrome
 * @description alphaNumericPalindrome should return true if the string has alphanumeric characters that are palindrome irrespective of special characters and the letter case.
 * @param {string} str the string to check
 * @returns {boolean}
 * @see [Palindrome](https://en.wikipedia.org/wiki/Palindrome)
 * @example
 * The function alphaNumericPalindrome() receives a string with varying formats
 * like "racecar", "RaceCar", and "race CAR"
 * The string can also have special characters
 * like "2A3*3a2", "2A3 3a2", and "2_A3*3#A2"
 *
 * But the catch is, we have to check only if the alphanumeric characters
 * are palindrome i.e remove spaces, symbols, punctuation etc
 * and the case of the characters doesn't matter
 */
const alphaNumericPalindrome = (str) => {
  if (typeof str !== 'string') {
    throw new TypeError('Argument should be string')
  }

  // removing all the special characters and turning everything to lowercase
  const newStr = str.replace(/[^a-z0-9]+/gi, '').toLowerCase()
  const midIndex = newStr.length >> 1 // x >> y = floor(x / 2^y)

  for (let i = 0; i < midIndex; i++) {
    if (newStr.at(i) !== newStr.at(~i)) {
      // ~n = -(n + 1)
      return false
    }
  }

  return true
}

export default alphaNumericPalindrome

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Coerce with String(): alphaNumericPalindrome(String(value)).
  2. Avoid String wrapper objects; use string literals.
  3. Type-guard at the boundary: if (typeof str === 'string').

Example fix

// before
alphaNumericPalindrome(userId) // userId is a number

// after
alphaNumericPalindrome(String(userId))
Defensive patterns

Strategy: type-guard

Validate before calling

function safeAlphaNumericPalindrome(value) {
  if (typeof value !== 'string') {
    throw new TypeError('Expected a string')
  }
  return alphaNumericPalindrome(value)
}

Type guard

function isString(v) {
  return typeof v === 'string'
}

Try / catch

try {
  alphaNumericPalindrome(input)
} catch (e) {
  if (e instanceof TypeError && e.message === 'Argument should be string') {
    return alphaNumericPalindrome(String(input))
  }
  throw e
}

Prevention

When it happens

Trigger: Calling alphaNumericPalindrome(12321), alphaNumericPalindrome(['r','a','c','e','c','a','r']), alphaNumericPalindrome(null), or alphaNumericPalindrome(new String('racecar')) — the last fails because typeof new String() === 'object'.

Common situations: Numeric IDs that look palindromic; String wrapper objects from older codebases; untyped form input parsed as a number.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/9cb25a4ef969e97d. Report an issue: GitHub.