{"record":{"id":"83da5307bee4c353","repo":"TheAlgorithms/JavaScript","slug":"strings-must-be-of-the-same-length","errorCode":null,"errorMessage":"Strings must be of the same length","messagePattern":"Strings must be of the same length","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"String/HammingDistance.js","lineNumber":20,"sourceCode":" * Hamming Distance: https://en.wikipedia.org/wiki/Hamming_distance\n *\n *\n * Hamming distance is a metric for comparing two binary data strings.\n *\n * While comparing two binary strings of equal length, Hamming distance\n * is the number of bit positions in which the two bits are different.\n * The Hamming distance between two strings, a and b is denoted as d(a,b)\n */\n\n/**\n * @param {string} a\n * @param {string} b\n * @return {number}\n */\n\nexport const hammingDistance = (a, b) => {\n  if (a.length !== b.length) {\n    throw new Error('Strings must be of the same length')\n  }\n\n  let distance = 0\n\n  for (let i = 0; i < a.length; i += 1) {\n    if (a[i] !== b[i]) {\n      distance += 1\n    }\n  }\n\n  return distance\n}\n","sourceCodeStart":2,"sourceCodeEnd":33,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/HammingDistance.js#L2-L33","documentation":"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.","triggerScenarios":"Calling hammingDistance('abc','ab'), hammingDistance('karolin','kathrin'), hammingDistance('10100','101'), hammingDistance([1,2,3],[1,2]). Any two array-likes whose .length differs.","commonSituations":"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.","solutions":["Pad or trim both inputs to equal length before calling.","Pre-validate length equality and surface a clearer error upstream.","If comparing code points, normalize both with Array.from(str) so multi-byte chars are counted consistently."],"exampleFix":"// before\nhammingDistance(a, b)\n\n// after\nif (a.length === b.length) hammingDistance(a, b)\nelse throw new Error(`length mismatch: ${a.length} vs ${b.length}`)","handlingStrategy":"validation","validationCode":"if (typeof a !== 'string' || typeof b !== 'string') {\n  throw new TypeError('both inputs must be strings')\n}\nif (a.length !== b.length) {\n  throw new Error(`length mismatch: ${a.length} vs ${b.length}`)\n}\nhammingDistance(a, b)","typeGuard":"const areEqualLengthStrings = (a, b) => typeof a === 'string' && typeof b === 'string' && a.length === b.length","tryCatchPattern":"try {\n  hammingDistance(a, b)\n} catch (e) {\n  if (e instanceof Error && /same length/i.test(e.message)) { /* pad/trim and retry */ } else throw e\n}","preventionTips":["Normalize both inputs (trim, pad, same encoding) before comparing.","Add a typeof guard — the library does NOT type-check before reading .length.","For Unicode, compare via Array.from(str) to count code points."],"tags":["string","validation","hamming-distance","length-mismatch"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}