How to understand a legacy JavaScript codebase
Old jQuery, CommonJS, callbacks, global variables and build tools no one remembers: a practical approach to understanding and safely changing legacy JavaScript.
Legacy JavaScript has its own textures: global variables shared across script tags, callbacks nested five deep, require mixed with import, a build tool configured years ago by someone who's left. It still runs the business. Here's how to understand it without breaking it.
Expect several eras in one codebase
Most legacy JavaScript isn't one style but layers of them: jQuery from 2013, a Backbone or AngularJS app from 2016, a React rewrite of half the screens from 2020. Before reading any feature, identify the eras:
- Script tags in HTML templates loading files in order
- CommonJS (
require,module.exports) versus ES modules (import) - Callbacks, then promises, then
async/await - Old frameworks (jQuery, Backbone, AngularJS) next to new ones
Knowing which era a file belongs to tells you which rules apply in it.
Find out how code actually loads
In older apps there may be no single entry point:
- Script tags. The order in the HTML is the dependency order, and every top-level
varis a global. - Concatenation builds. Grunt or Gulp may join files into one bundle in a configured order.
- Old bundlers. An early webpack or Browserify config may define aliases you won't find anywhere else.
Read the build config and the HTML templates before the JavaScript.
Track globals
Globals are the hidden wiring of legacy code. Search for assignments to window., top-level var declarations in non-module scripts, and jQuery plugins attached with $.fn.. Make a list of each global, where it's set and where it's read.
Follow callbacks carefully
Nested callbacks are hard to read because execution order differs from reading order. When tracing, number each callback in the order it will actually run. With jQuery, also watch for event handlers bound with .on() to elements that are created later; their code runs far from where it's defined.
Look for the real data flow
Legacy front ends often store state in the DOM itself: data attributes, hidden inputs, CSS classes used as flags. When a value seems to come from nowhere, search for where the DOM is changed.
Add tests before changes
Legacy code often lacks tests. Before changing a function, write characterisation tests that record what it does now, even if what it does seems wrong. Then change it. This is the safest single habit for legacy work.
Make small, reversible changes
- Prefer adding a new function next to the old one over rewriting it.
- Keep behaviour identical in refactors, and change behaviour in separate commits.
- Remove dead code only after confirming it's dead. See finding dead code.
Use history
git log and git blame explain a lot about legacy code: why a strange workaround exists, which bug a condition fixed. See code archaeology with git.
Where AI helps
AI tools are good at explaining dense legacy patterns (prototype chains, arguments tricks, jQuery idioms) in plain language. RepoGrasp reads .js files, so you can ask "where is window.cartState set and read?" and get cited answers across files. Dynamic tricks like eval or computed global names are hard for any static reader, so confirm those by running the code.