TheAlgorithms/JavaScript · error · Error

Strings must be of the same length

Error message

Strings must be of the same length

What it means

Guard in hammingDistance. The function counts positions where two equal-length strings differ and throws a plain Error (not TypeError) if a.length !== b.length. Importantly, there is NO type check first: passing null/undefined throws a different error ('Cannot read properties of null (reading length)') rather than this message. This message only appears when both inputs ARE strings (or array-likes) of differing length.

Source

Thrown at String/HammingDistance.js:20

 * Hamming Distance: https://en.wikipedia.org/wiki/Hamming_distance
 *
 *
 * Hamming distance is a metric for comparing two binary data strings.
 *
 * While comparing two binary strings of equal length, Hamming distance
 * is the number of bit positions in which the two bits are different.
 * The Hamming distance between two strings, a and b is denoted as d(a,b)
 */

/**
 * @param {string} a
 * @param {string} b
 * @return {number}
 */

export const hammingDistance = (a, b) => {
  if (a.length !== b.length) {
    throw new Error('Strings must be of the same length')
  }

  let distance = 0

  for (let i = 0; i < a.length; i += 1) {
    if (a[i] !== b[i]) {
      distance += 1
    }
  }

  return distance
}

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Pad or trim both inputs to equal length before calling.
  2. Pre-validate length equality and surface a clearer error upstream.
  3. If comparing code points, normalize both with Array.from(str) so multi-byte chars are counted consistently.

Example fix

// before
hammingDistance(a, b)

// after
if (a.length === b.length) hammingDistance(a, b)
else throw new Error(`length mismatch: ${a.length} vs ${b.length}`)
Defensive patterns

Strategy: validation

Validate before calling

if (typeof a !== 'string' || typeof b !== 'string') {
  throw new TypeError('both inputs must be strings')
}
if (a.length !== b.length) {
  throw new Error(`length mismatch: ${a.length} vs ${b.length}`)
}
hammingDistance(a, b)

Type guard

const areEqualLengthStrings = (a, b) => typeof a === 'string' && typeof b === 'string' && a.length === b.length

Try / catch

try {
  hammingDistance(a, b)
} catch (e) {
  if (e instanceof Error && /same length/i.test(e.message)) { /* pad/trim and retry */ } else throw e
}

Prevention

When it happens

Trigger: Calling hammingDistance('abc','ab'), hammingDistance('karolin','kathrin'), hammingDistance('10100','101'), hammingDistance([1,2,3],[1,2]). Any two array-likes whose .length differs.

Common situations: Comparing sequences (DNA, binary, codes) where one side was truncated or padded differently; one input trimmed and the other not; whitespace/newline differences changing length; Unicode code points vs UTF-16 units mismatching lengths.

Related errors


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