TheAlgorithms/JavaScript · error · TypeError

Input must be a string or a number

Error message

Input must be a string or a number

What it means

Guard in isPalindromeIterative. Unlike the other string utilities, this one accepts EITHER a string OR a number (it calls .toString() internally). It throws TypeError only for inputs that are neither — booleans, objects, arrays, null, undefined, functions, symbols all throw.

Source

Thrown at String/IsPalindrome.js:21

 * @description isPalindromeIterative function checks whether the provided input is palindrome or not
 * @param {String | Number} x - The input to check
 * @return {boolean} - Input is palindrome or not
 * @see [Palindrome](https://en.wikipedia.org/wiki/Palindrome)
 */

/*
  * Big-O Analysis
      * Time Complexity
        - O(N) on average and worst case scenario as input is traversed in linear fashion
        - O(1) on best case scenario if the input already is a string (otherwise toString() method takes O(N))
               and the first & last characters don't match, triggering an early return
      * Space Complexity
        - O(1)
*/

export function isPalindromeIterative(x) {
  if (typeof x !== 'string' && typeof x !== 'number') {
    throw new TypeError('Input must be a string or a number')
  }

  // Convert x to string whether it's number or string
  const string = x.toString()
  const length = string.length

  if (length === 1) return true

  // Apply two pointers technique to compare first and last elements on each iteration
  for (let start = 0, end = length - 1; start < end; start++, end--) {
    // Early return if compared items are different, input is not a palindrome
    if (string[start] !== string[end]) return false
  }
  // If early return in condition inside for loop is not reached, then input is palindrome
  return true
}

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Pass a string or number; coerce first with String(value) for safety.
  2. Pre-validate: typeof x === 'string' || typeof x === 'number'.
  3. Reject objects/arrays explicitly before the call.

Example fix

// before
isPalindromeIterative(maybeValue)

// after
const t = typeof maybeValue
if (t === 'string' || t === 'number') isPalindromeIterative(maybeValue)
else throw new TypeError('expected string or number, got ' + t)
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof x !== 'string' && typeof x !== 'number') {
  throw new TypeError('x must be a string or number')
}
isPalindromeIterative(x)

Type guard

const isStringOrNumber = (v) => typeof v === 'string' || typeof v === 'number'

Try / catch

try {
  isPalindromeIterative(x)
} catch (e) {
  if (e instanceof TypeError) { /* not string/number */ } else throw e
}

Prevention

When it happens

Trigger: Calling isPalindromeIterative(null), isPalindromeIterative(undefined), isPalindromeIterative(true), isPalindromeIterative({}), isPalindromeIterative([1,2,1]), isPalindromeIterative(Symbol()).

Common situations: A value read from a loosely-typed source (form, JSON) where the type is unknown; passing an array of digits thinking it will be compared element-wise; a boolean flag accidentally forwarded.

Related errors


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