Explain This Code

Paste code and get a plain-English explanation of every line plus a one-paragraph summary of what the whole program does. JavaScript and Python are parsed and run, so the explanation includes the real output; C, Java, C#, PHP, SQL, Bash, HTML and CSS are explained from recognised patterns.

Analyzer Code Lab Updated Oct 5, 2026
How to Use
  1. Paste a snippet into the box, or pick one of the five presets. The factorial preset is already explained when the page opens.
  2. The language is detected automatically from clues in the code. If the guess is wrong, choose the language from the menu and the code is explained again.
  3. Press Explain (Ctrl+Enter). The readouts show the language, how many lines were explained, the functions it defines and what it prints.
  4. Read the summary first, then each row: the code on top, the plain-English explanation underneath, indented to match the code’s nesting. Rows marked pattern-based come from line patterns, and a line no pattern recognises is left unexplained rather than guessed.
  5. For JavaScript and Python press Step through it in the Visualizer to watch it run line by line; if it stops with an error, the output card links Explain This Error. Show Work lists the rule behind every row.
Code
any language · Ctrl+Enter
Presets
Explanation
Parsed: syntax tree + real run

This JavaScript program defines a function factorial(n) that calls itself (recursion) until n <= 1, then prints factorial(5), which is 120.

Language
JavaScript100%
Lines explained
7of 7
Functions
1factorial
Output
1201 line
Explaining the factorial preset…

Worked Example

Recursion. factorial(n) returns 1 when n <= 1 and otherwise returns n * factorial(n - 1). Calling factorial(5) starts a chain of calls: factorial(5), factorial(4), factorial(3), factorial(2) and factorial(1), which is 5 calls. The check n <= 1 runs once per call, so 5 times: false for 5, 4, 3 and 2, true only for 1, the base case that stops the chain. The answers then come back up: 1, 2 × 1 = 2, 3 × 2 = 6, 4 × 6 = 24 and 5 × 24 = 120. The tool reports exactly these numbers from its run (5 calls; true once, false 4 times; prints 120 after 24 recorded steps).

A closure. makeCounter() creates count = 0 and returns an inner function that adds 1 to count and gives it back. const next = makeCounter(); keeps that inner function, and with it access to its own count, even though makeCounter has finished. The two bare next(); calls give back 1 and then 2, which are thrown away because nothing stores or prints them; console.log(next()) makes count 3 and prints 3. Reading it as “count is reset to 0 on every call” would predict 1, which is why this snippet confuses people.

The common mistake: an off-by-one loop. With const arr = [5, 10, 15]; the loop for (let i = 0; i <= arr.length; i++) runs while i is 0, 1, 2 and 3: 4 passes for 3 items. The last pass reads arr[3], which does not exist and is undefined, so sum goes 5, 15, 30 and then 30 + undefined = NaN. The tool flags the <= with .length as one past the last item and shows the printed NaN; with i < arr.length the loop makes 3 passes and prints 30. The C preset is not run here, but it is easy to check by hand: 4 + 8 + 15 + 16 + 23 = 66, so printf("Sum: %d\n", sum) would print Sum: 66.

Show Work

The factorial preset is explained when the page loads; the language clues, the method and the rule behind every row appear here.

How It Works

JavaScript and Python
code → tokens → syntax tree
One sentence rule per statement node (declaration, if, for, while, function, return, call, assignment)
Run facts
≤ 5,000 steps · ≤ 250 nested calls
The sandboxed stepper records each print, loop pass, if-check and call, and the explanation quotes them
Other languages
line → first matching pattern
No match means no explanation: the line is marked not recognised, never guessed
Lines explained
explained ÷ non-blank lines
A line counts when it is inside an explained statement, a comment or a closing bracket
Language
confidence = best ÷ total points
The Code Inspector’s clue table: console.log( is 4 points for JavaScript, #include <stdio.h> is 6 for C
SQL processing order
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT
The order the PostgreSQL documentation gives for processing a SELECT

Two kinds of explanation. JavaScript and Python inside the Code Lab’s teaching subset are parsed, so every sentence comes from the exact structure of the code: which variable a loop counts with and between which bounds, which branch an else belongs to, whether a function calls itself or returns another function. The same engine then runs the program, so explanations quote the real output (“When run, this prints 120”), how many passes each loop made and how often each check was true. Code outside the subset, such as classes, list comprehensions or f-strings, falls back to line patterns and the page says so. Everything else is explained by line patterns per language and is labelled pattern-based — may miss details.

Where the language facts come from. JavaScript: MDN Web Docs (closures, map, filter, reduce, sort, which compares items as strings unless given a compare function, slice, indexOf, String.replace, which replaces only the first match of a string pattern, let, const, strict and loose equality, hoisting and Math.random). Python: the Python tutorial and library reference (range never includes its end point, list comprehensions, / always returns a float and // floors, negative indexes count from the right, docstrings, str.replace replaces every occurrence, split() with no separator splits on runs of whitespace, dict.get defaults to None, list.sort sorts in place, round() rounds halves to even). C: cppreference for the printf conversions (%d, %f with 6 decimals by default, %c, %s), the main function and its zero exit status, and switch fall-through. SQL: the PostgreSQL documentation for SELECT processing, joins, count(*), LIKE wildcards, BETWEEN (inclusive), PRIMARY KEY, and UPDATE, DELETE and DROP TABLE. Java’s main method follows the Oracle Java Tutorials, C#’s Main the Microsoft C# documentation, PHP’s foreach the PHP Manual, and shell syntax, test operators, pipelines and command substitution the GNU Bash manual, with the GNU Coreutils manual for ls, cp, mv, rm, mkdir, cat, head, tail, sort, uniq, wc, chmod and touch.

What it cannot tell. A pattern sees one line at a time, so it does not know what a variable holds, which overload a method call picks, or what a macro expands to. It never runs C, Java, SQL or shell commands, and the JavaScript and Python stepper runs a teaching subset, so a program that relies on something outside it is explained without its output. The explanation describes what the code does, not whether that is what you meant: an off-by-one is only flagged in the common <= .length form.

Background: Code Is Written for People

The idea that a program should explain itself is old. The preface to the first edition of Abelson and Sussman’s Structure and Interpretation of Computer Programs (MIT Press) puts it as a principle: “programs must be written for people to read, and only incidentally for machines to execute.” In 1984 Donald E. Knuth published “Literate Programming” in The Computer Journal 27(2), pages 97–111 (doi:10.1093/comjnl/27.2.97); his description of the later book of the same name says the main idea is “to treat a program as a piece of literature, addressed to human beings rather than to a computer.”

Reading code is also a skill that computing education studies on its own, separately from writing it. In 2004 an ITiCSE working group of 12 authors led by Raymond Lister published “A multi-national study of reading and tracing skills in novice programmers” (ITiCSE working group reports, pages 119–150, doi:10.1145/1044550.1041673). Tracing what a program does step by step is what Philip Guo’s Online Python Tutor automated (Proceedings of the 44th ACM Technical Symposium on Computer Science Education, 2013, pages 579–584, doi:10.1145/2445196.2445368); the Code Lab’s Execution Visualizer does the same in your browser, and this page adds the sentence-by-sentence reading on top of the same engine.

About This Tool

Reference: Marlin G-code Reference.

Reference: M-Code Reference by Controller.

Explain This Code turns a snippet into plain English: a one-paragraph summary of what the whole program does, then an explanation of every statement, indented to match the code, with notes on the operators and built-in functions it uses. For JavaScript and Python it also runs the code and quotes the real output, the number of loop passes and how often each condition was true. It reads 16 languages and explains C, C++, Java, C#, PHP, SQL, Bash, HTML, CSS and JSON from recognised patterns.

Most code explainers produce fluent text that can be confidently wrong. This one only says what a rule or the syntax tree supports, marks anything it does not recognise, labels pattern-based explanations as such, and shows the rule behind every row in Show Work. It also connects to the rest of the Code Lab: step through the same code in the Execution Visualizer, measure it in the Code Inspector, or decode an error message in Explain This Error. Everything runs in your browser; nothing you paste is uploaded or stored.

It is for beginners reading their first programs, students checking homework, developers handed code in an unfamiliar language, and anyone who has pasted an AI-generated snippet and wants to know what it actually does.

Related tools: Code Inspector, Code Execution Visualizer, and Explain This Error.

Frequently Asked Questions

How does it explain JavaScript and Python?

It parses the code into a syntax tree with the Code Lab’s parser and has one rule per kind of statement: a declaration, an if, a loop, a function definition, a return, a call. Operators, indexes and built-in calls inside a statement add short notes, such as “% is the remainder”. Then it runs the program in the same sandboxed stepper the Visualizer uses, so the explanation says what really happened: in the factorial preset the function is called 5 times, the check n <= 1 is true once and false 4 times, and it prints 120 after 24 steps.

What does “pattern-based — may miss details” mean?

C, C++, Java, C#, PHP, SQL, Bash, HTML, CSS and JSON are not parsed. Each line is matched against rules for constructs the tool knows, such as a printf call, a for header, a SQL clause or an HTML tag, and the first rule that matches writes the sentence. A line that no rule matches is shown as not recognised instead of being guessed. In the C preset all 10 non-blank lines are recognised, including printf("Sum: %d\n", sum), where %d is replaced by sum as a whole number.

Is it safe to paste code? Does it run it?

Nothing is uploaded; everything happens in your browser. JavaScript and Python are run by the Code Lab’s own interpreter, not by the browser’s eval, and it stops after 5,000 steps or 250 nested calls, so while (true) {} ends with a message instead of freezing the page. Other languages are only read, never run.

Why does the output say “—”?

The code could not be run here, and the output card says why. C, C++, Java, C#, Go and Rust need a compiler, PHP needs a server, SQL needs a database that holds the tables, and HTML and CSS are drawn by a browser rather than run. JavaScript or Python that uses something the stepper does not support yet, such as .reduce() or a class, is still explained but not run. A program that runs and then hits an error shows Error with the line number instead, plus a link to Explain This Error.

Why does SELECT come first if the database works it out later?

SQL is written in one order and processed in another. The PostgreSQL documentation for SELECT lists the steps: the FROM list (with its joins), then WHERE, then GROUP BY and HAVING, then the output columns of the SELECT list, then DISTINCT, ORDER BY and LIMIT. In the SQL preset that means the 2 tables are joined, rows with orders.total of 50 or less are dropped, the rest are grouped by name, groups with fewer than 2 rows are dropped, and only then are name and COUNT(*) built, sorted highest first and cut to 5 rows.

How do I use the Explain This Code?

Simply type your numbers and read the result, which refreshes the instant you change something. There is nothing to submit and nothing to wait for.

Do I need to install or sign up for anything?

Not at all — it runs in the browser with nothing to install and no account. After it loads once, it even works without an internet connection.

Is my information private?

Yes. Everything happens in your browser. Nothing you type is sent to a server or saved anywhere.

Common Use Cases

Understand an AI-generated snippet

Paste the closure preset: the tool explains that next() keeps its own count, that the first two calls give back 1 and 2 and throw them away, and that the program prints 3.

Check homework before handing it in

The Python list preset adds 4 scores to 311, keeps the 3 that are at least 70 and prints Average: 77.75, with every line and the real output explained.

Read an unfamiliar SQL query

An 8-line query with a JOIN, WHERE, GROUP BY, HAVING, ORDER BY and LIMIT 5 gets one sentence per clause plus the order the database runs them in.

Catch an off-by-one loop

for (let i = 0; i <= arr.length; i++) over 3 items is flagged: it makes 4 passes, reads arr[3], which is undefined, and the sum comes out NaN instead of 30.

Learn a language you cannot read yet

A 17-line C# program with braces on their own lines gets all 16 non-blank lines labelled, from using System; through Console.ReadLine and Console.WriteLine to each of its 4 closing braces, with no compiler needed.

Last updated: