← Back to Blog

Building a Diff Checker with jsdiff: Line, Word, and Character Diffs

May 4, 2026 3 min read By CodeTidy Team

The Frustration of Manual Code Reviews

Have you ever spent hours manually reviewing code changes, trying to spot the differences between two versions of a file? We've all been there, and it's a tedious process that can lead to mistakes and missed changes. But what if we could automate this process and make it easier to review code changes?

Table of Contents

  • Understanding the Problem and the Solution
  • Setting Up the Diff Checker with jsdiff
  • Computing Line, Word, and Character Diffs
  • Creating a Side-by-Side View
  • Highlighting Additions and Removals
  • Key Takeaways
  • FAQ

Understanding the Problem and the Solution

When reviewing code changes, we need to be able to quickly and accurately identify the differences between two versions of a file. This is where a diff checker comes in – a tool that can automatically highlight the changes between two pieces of text. There are many diff checker libraries available, but in this tutorial, we'll be using jsdiff, a popular and lightweight JavaScript library for computing diffs.

Setting Up the Diff Checker with jsdiff

To get started with jsdiff, we need to include the library in our project. We can do this by installing it via npm or yarn:

npm install jsdiff

Once installed, we can import the library and start using it to compute diffs. Here's an example of how to use jsdiff to compute the diff between two strings:

const jsdiff = require('jsdiff');

const oldText = 'This is the original text';
const newText = 'This is the updated text';

const diff = jsdiff.diffLines(oldText, newText);

console.log(diff);

This will output the diff between the two strings, showing us the additions and removals.

Computing Line, Word, and Character Diffs

jsdiff provides three main functions for computing diffs: diffLines, diffWords, and diffChars. Each function takes two strings as input and returns the diff between them.

  • diffLines computes the diff between two strings on a line-by-line basis.
  • diffWords computes the diff between two strings on a word-by-word basis.
  • diffChars computes the diff between two strings on a character-by-character basis.

Here's an example of how to use each function:

const lineDiff = jsdiff.diffLines(oldText, newText);
const wordDiff = jsdiff.diffWords(oldText, newText);
const charDiff = jsdiff.diffChars(oldText, newText);

console.log(lineDiff);
console.log(wordDiff);
console.log(charDiff);

Creating a Side-by-Side View

To make it easier to review code changes, we can create a side-by-side view of the two versions of the file. We can do this by using a library like diff2html, which takes the diff output from jsdiff and renders it as a side-by-side view.

Here's an example of how to use diff2html to create a side-by-side view:

const diff2html = require('diff2html');

const diffHtml = diff2html(diff);

console.log(diffHtml);

This will output the HTML for the side-by-side view, which we can then render in the browser.

Highlighting Additions and Removals

To make it easier to see what's changed, we can highlight the additions and removals in the side-by-side view. We can do this by adding some CSS to our HTML.

Here's an example of how to highlight additions and removals:

.addition {
  background-color: #cff;
}

.removal {
  background-color: #fcc;
}

We can then add these classes to the HTML elements that represent the additions and removals.

Key Takeaways

  • Use jsdiff to compute the diff between two versions of a file.
  • Use diffLines, diffWords, and diffChars to compute the diff on a line-by-line, word-by-word, and character-by-character basis.
  • Use diff2html to create a side-by-side view of the two versions of the file.
  • Highlight additions and removals using CSS.

FAQ

Q: What is the difference between diffLines, diffWords, and diffChars?

A: diffLines computes the diff on a line-by-line basis, diffWords computes the diff on a word-by-word basis, and diffChars computes the diff on a character-by-character basis.

Q: How do I render the side-by-side view in the browser?

A: You can use a library like diff2html to render the side-by-side view in the browser.

Q: Can I customize the appearance of the side-by-side view?

A: Yes, you can customize the appearance of the side-by-side view by adding CSS to your HTML.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp