Find difference between two strings in JavaScript
Codemia Knowledge Hub ·
Introduction
Finding the difference between two strings in JavaScript depends on what kind of difference you need. For character-level diffs, iterate through both strings and compare at each index. For word-level or line-level diffs, split the strings and compare the resulting arrays. For production use cases like showing text changes (similar to Git diffs), use a library like diff (npm). JavaScript has no built-in diff function, so you must implement or import one.
Character-by-Character Comparison
This identifies positions where characters differ. It handles strings of different lengths by treating missing characters as differences.
Finding Added/Removed Characters
This compares character sets — it tells you which characters exist in one string but not the other, without considering position.
Word-Level Difference
Longest Common Subsequence (LCS)
The LCS algorithm finds the longest sequence of characters common to both strings, which helps identify what was kept versus what changed:
The LCS is the foundation of diff algorithms. Characters not in the LCS are the "differences."
Using the diff Library
For production applications, use the diff npm package:
Levenshtein Distance (Edit Distance)
The edit distance tells you the minimum number of single-character edits (insertions, deletions, substitutions) to transform one string into another:
Highlighting Differences in HTML
Comparing Strings Ignoring Case/Whitespace
Common Pitfalls
- Unicode and emoji: String indexing in JavaScript uses UTF-16 code units. Characters like emoji (
"😀".length === 2) span two indices. UseArray.from(str)or the spread operator[...str]to split by code points instead of code units. - Performance with long strings: Character-by-character comparison is O(n), but LCS and Levenshtein are O(n*m). For very long strings (10,000+ characters), these algorithms use significant memory and time. Use the
difflibrary which has optimized implementations. - Set-based comparison loses position: Using
Setto find unique characters tells you what changed but not where. For positional differences, use index-based comparison or a diff algorithm. - Case sensitivity:
'Hello' !== 'hello'in JavaScript. If case differences are not meaningful, normalize both strings with.toLowerCase()before comparing. - Whitespace differences: Trailing spaces, different line endings (
\nvs\r\n), and tab-vs-space differences create noise. Normalize whitespace before comparing if these differences are not relevant.
Summary
- For character-by-character differences, iterate with index comparison
- For word-level differences, split strings and compare arrays
- Use the
diffnpm package for production-quality diffs (character, word, or line level) - Levenshtein distance measures how many edits are needed to transform one string into another
- LCS finds the longest common subsequence, which is the basis of most diff algorithms
- Normalize strings (case, whitespace) before comparing when those differences are irrelevant
Related reading
- Find the min/max element of an array in JavaScript
- Finding All Combinations Cartesian product of JavaScript array values
- Finding element nearest to clicked point
- Fixed position but relative to container
- Flexbox center horizontally and vertically
- force browsers to get latest js and css files in asp.net application
- Forcing a function to wait until another function is complete
- foreach vs someList.ForEach
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.