You build a source code editor in JavaScript by layering four concerns: a text input model that survives the browser's quirks, a rendering layer that shows syntax colors without destroying the cursor, a state layer that tracks edits for undo and change events, and a language layer that adds completion, bracket matching, and error hints. The condition that shapes every decision is whether you're writing a training-ground editor or something that has to survive years of real use at scale. For learning and small tools, a transparent over a highlighted preview is the fastest credible path; for production, you either adopt an established engine like CodeMirror 6 or Monaco or commit to building a serious piece of infrastructure. This article walks through both, with runnable code and honest failure modes.
Decide the architecture before writing a line
The single most expensive mistake is starting with a naive contenteditable div because it looks easy. The browser owns selection inside contenteditable, and it will insert and elements mid-typing, fight you over whitespace, and break caret position on undo in ways that vary by engine. Every mature editor that isn't textarea-based (Monaco, CodeMirror, Ace) has years of patches devoted to browser input behavior. That tells you the real shape of the problem.
Three workable architectures exist:
The overlay approach. A real receives keystrokes while a styled sits exactly behind it, painted with syntax colors. Selection, IME composition, mobile keyboards, clipboard, and screen readers all work because the browser is editing genuine text. Cursor styling comes from CSS on the textarea. The trade-off is that you cannot color a caret or paint decorations over the text inside a textarea, so features like inline error squiggles or multi-caret editing become impossible or hacky.
The custom rendering approach. Keystrokes are captured on a hidden input or a focused container, text lives in a JavaScript document model, and every visible line is painted as HTML. This gives total control: multiple selections, gutter icons, inline widgets, diff views. The cost is that you now own selection painting, scrolling synchronization, IME support, accessibility, and clipboard semantics. Budget weeks, not hours.
The engine approach. Load CodeMirror 6, Monaco, or Ace and configure them. You write extensions and theme wiring rather than the core. This is the right answer for almost anyone shipping a product, and even understanding it is easier once you know the mechanics below.
If you're learning or building a small playground, choose overlay. If you're building an editor as product-grade infrastructure, use an engine. Build custom rendering only when you have a specific interaction the engines can't express.
The minimal working editor: prerequisites and setup
You need a modern browser with ES modules, a text file with index.html, and no build tooling for the first version. About sixty lines gets you a functional highlighted editor. Here's the prerequisite-then-action sequence and what each step should produce.
Step 1: Lay out the two layers. The textarea and the highlight layer must have identical typography, padding, and box dimensions, or the cursor will drift away from the colored text. The textarea goes on top with color: transparent and a visible caret, so its glyphs are invisible but its insertion point isn't.
Success check: type into the editor; text appears in the highlight layer while the native caret blinks in the expected position. If the caret sits one character or half a line off, your fonts or paddings differ between layers.
Step 2: Escape HTML before highlighting. Never interpolate raw source into innerHTML. A string like