Text Diff Explained
A diff checker lines up two texts and marks every line that was removed (red, minus) or added (green, plus), using the smallest possible set of changes. Paste the original on the left and the new version on the right, then pick line diff for code or word diff to see exactly which words changed. Everything runs in your browser.
A diff (short for difference) algorithm compares two texts and identifies what changed between them. The classic algorithm, Longest Common Subsequence (LCS), finds the longest sequence of lines that appear in both texts in the same order: lines not in this sequence are either added or deleted.
The Unix diff command has been the standard for code comparison since 1974. Git, GitHub, and virtually all version control systems use diff under the hood to show what changed between commits.
How Diff Works
Line vs Word Diff
Use Cases
Privacy
Worked Example: the Sample Texts
The two texts loaded on this page differ in three places. Line diff reports 3 lines deleted, 3 added and 2 unchanged: lines 1, 4 and 5 of A were replaced, while "Hello world" and "Line three" match.
| Line | Line diff shows | Word diff marks |
|---|---|---|
| 1 | Whole line removed and added | jumps to leaps, dog to cat |
| 2 and 3 | Unchanged | Unchanged |
| 4 | Whole line removed and added | will be deleted removed; is a brand new, added to B added |
| 5 | Whole line removed and added | A to B |
Word diff pairs the first removed line of a changed block with the first added line, the second with the second, and so on. Extra lines on either side are shown whole.
What Each Kind of Change Looks Like
| Change | Result | Tip |
|---|---|---|
| One word edited | 1 line removed, 1 added | Use word diff to see the word |
| Line inserted | 1 line added, nothing removed | Nothing to fix, it is a plain addition |
| Line moved | Removed in one place, added in another | Diffs do not detect moves |
| Indentation or trailing spaces | Lines look the same but are marked | Turn on Ignore whitespace |
| Tabs vs spaces | Marked as changed | Ignore whitespace treats any run of spaces or tabs as one space |
| Reformatted JSON | Many lines changed | Format both sides the same way first |
How the Comparison Works
The checker first skips the lines both texts share at the start and the end, then runs the Myers diff algorithm on what is left. Myers finds the shortest edit script, the fewest deletions and insertions that turn A into B, in time proportional to the text length times the number of differences, so two long files with a few edits compare almost instantly. Word diff runs the same algorithm again inside each changed line, on words, spaces and punctuation marks.
Line numbers refer to text A for removed and unchanged lines and to text B for added lines. Ignore whitespace compares lines after trimming them and collapsing runs of spaces and tabs, so a b and a b count as equal but ab and a b do not.
Tips for cleaner diffs
- Format structured data before comparing: run both versions through the JSON formatter or the SQL formatter with the same settings.
- Sort lists that have no meaningful order, such as dependency lists, so only real changes remain.
- Compare plain text, not rich text. Copying from Word or a PDF can add invisible characters such as non-breaking spaces.
Method and sources. Diff algorithm: E. W. Myers, "An O(ND) Difference Algorithm and Its Variations", Algorithmica 1 (1986). Output conventions (minus for removed, plus for added) follow POSIX diff and git. The worked example counts were produced by running this page's diff code in Node.js on the sample texts.
Frequently Asked Questions
How does a diff checker work?
A diff checker uses the Longest Common Subsequence (LCS) algorithm or the Myers diff algorithm to find the minimum number of insertions and deletions needed to transform text A into text B. Lines that appear in both texts in the same order are "context" (unchanged). Lines only in A are "deletions." Lines only in B are "insertions." The result is displayed with color coding: green for additions, red for deletions, white/gray for unchanged lines.
What is the difference between line diff and word diff?
Line diff marks an entire line as added or deleted if it changed at all: even a single character change shows the whole line as red (old) and green (new). Word diff goes deeper: it shows which specific words within a line changed, highlighting only the changed portion in a contrasting color. Word diff is more informative for prose and documentation. Line diff is standard for code where line-level context is more useful.
Is my text safe to compare here?
Yes: this diff checker runs completely in your browser (client-side JavaScript). No text is sent to any server. Your content is never transmitted, logged, or stored anywhere. This makes it safe for comparing confidential documents, private code, personal data, legal contracts, or any sensitive material. Refresh the page and all content is gone: nothing is retained.
How do I compare two files?
Open both files in a text editor, select all content (Ctrl+A or Cmd+A), copy (Ctrl+C), and paste into the respective panels. For code files: open in VS Code or any editor, select all, copy, paste. For PDFs: open in a PDF reader, select all text, copy, paste (formatting may vary). For Word documents: open, Ctrl+A, Ctrl+C, paste the plain text version. File upload is not currently supported: this tool works with pasted text.
What does the "ignore whitespace" option do?
When "ignore whitespace" is enabled, lines that differ only in leading/trailing spaces or internal whitespace are treated as identical. This is useful when comparing code that may have been reformatted (indentation changes, trailing spaces cleaned up) without logical changes. Without this option, a line with extra leading spaces is treated as different from the same line without them. In git, the closest equivalent is git diff -b (ignore changes in the amount of whitespace); git diff -w goes further and ignores all whitespace, even between words.
How do I compare two code files using Git?
If the files are in a Git repository: git diff file.txt shows unstaged changes. git diff HEAD~1 HEAD file.txt compares last two commits. git diff branch1..branch2 file.txt compares across branches. For two arbitrary files not in Git: diff file1.txt file2.txt (Unix/Mac) or FC file1.txt file2.txt (Windows). VS Code has a built-in diff view: Command Palette → "Compare Active File With..."
What do the colors mean in a diff output?
Green (+): Lines present in text B but not A, these were added. Red (-): Lines present in text A but not B, these were deleted. White/gray (no sign): Lines present in both texts, unchanged context. In word diff, green highlighting within a line shows added words; red strikethrough shows deleted words. This color convention follows the Unix diff standard and is used universally in Git, GitHub, GitLab, and all version control tools.
Can I use this to compare JSON or code?
Yes, paste any text-based format: JSON, YAML, XML, HTML, CSS, JavaScript, Python, SQL, or plain text. For JSON specifically, consider formatting both versions first (use the JSON Formatter on this site) so the line structure is consistent and meaningful differences are more visible. For structured data, even a small change like adding a comma or changing indentation can affect many lines in unformatted JSON but only one line in properly formatted JSON.
What is a unified diff format?
Unified diff is the standard output format of the Unix diff -u command and Git. It looks like: --- a/file.txt (original file), +++ b/file.txt (modified file), then "hunks" starting with @@ -lineA,count +lineB,count @@ showing context lines, lines starting with - (removed), and lines starting with + (added). This format is used in patch files, code review systems, and bug tracking. It's compact (shows only changed sections with context) versus showing the entire file.
How do I compare two versions of a Word document?
Word has a built-in track changes comparison: Review tab → Compare → Compare Documents, select the two versions and Word generates a marked-up document showing all changes. For a plain-text comparison, copy the text from both Word documents and paste into this tool. Microsoft Word's native comparison is better for documents with formatting, images, and tables, since it understands the document structure. This tool is better for comparing the raw text content when you don't need Word-specific formatting insights.
Does the diff checker detect moved lines?
No. Like diff and git, it reports a moved line as removed where it was and added where it is now, because the shortest edit script is built from deletions and insertions only. If you moved a block of code, expect to see it twice, once in red and once in green.
Why is a line marked as changed when it looks identical?
The difference is usually invisible: trailing spaces, a tab instead of spaces, a non-breaking space pasted from a word processor, or different line endings. Turn on Ignore whitespace to skip spacing differences; in word diff the changed spot is highlighted even when it is only whitespace next to a word.