Code Execution Visualizer
Run JavaScript or Python one step at a time. At every step you see the line being run, each variable, the call stack and the output, with a plain-English note of what the step did, and you can step backwards as easily as forwards.
How to Use
- Pick JavaScript or Python, then an example. Each example opens already stepped to a moment worth seeing: factorial opens at its base case, five calls deep.
- Or type your own code in the editor and press Visualize (Ctrl+Enter). If it does not parse, the line and the reason are shown.
- Move with ◀ / ▶, the arrow keys, the slider, ▶ Play, or by clicking the call-depth timeline. The current line is highlighted in the listing.
- Read the Call stack: each function call is its own frame with its own variables, newest on top. The four readouts show the step, the line, how many calls deep you are and how many variables are in scope.
- Scroll to Show Work for a plain-English log of every step up to the one on screen, such as “Check n <= 1 → true, so run the if block.”
Worked Example
Recursion: factorial(5) in JavaScript. The run takes 24 steps. Step 1 starts the program and step 2 runs console.log(factorial(5)), which has to call factorial(5) first. Each call from n = 5 down to n = 2 takes three steps (the call, the check n <= 1 → false, and the return line, which must wait for the next call), so those four calls use steps 3–14. factorial(1) is called at step 15, five frames deep; its check is true at step 16 and it returns 1 at step 17. Then the stack unwinds, one frame per step: 2 × 1 = 2, 3 × 2 = 6, 4 × 6 = 24 and 5 × 24 = 120 (steps 18–22). Step 23 prints 120 and step 24 ends the program: 1 + 1 + 4 × 3 + 3 + 5 + 1 + 1 = 24.
A loop: summing 1 to 5. for (let i = 1; i <= 5; i++) runs its test six times: true for i = 1, 2, 3, 4 and 5, then false at i = 6. After each pass the total reads 1, 3, 6, 10 and 15. Add the two declarations, the five additions, the console.log line and its output, and the start and end steps, and the whole run is 2 + 6 + 5 + 2 + 2 = 17 steps. The output is sum: 15.
The common mistake: an off-by-one test. Write i < 5 instead of i <= 5 and the trace shows the last test as “After i++, i = 5: test i < 5 → false, so the loop ends after 4 passes”. The total stops at 1 + 2 + 3 + 4 = 10, not 15, and the run is 15 steps instead of 17. The program runs without an error, which is why this bug is easy to miss and easy to see once you step through it.
Show Work
How It Works
Supported language. JavaScript: let, const and var; numbers, strings, booleans, null, undefined, arrays and objects; arithmetic including ** and the compound assignments; comparison and logical operators; if/else, while and C-style for loops with break and continue; function declarations, function expressions, arrow functions and recursion; push, pop, map, filter, forEach, join, slice, indexOf and includes; console.log and Math. Python: def and return, if/elif/else, while, for over range(), lists, strings and dicts, in and not in, and/or/not, ** and //, print, len, range, str, int, float, abs, sum, min and max, with Python’s own % for negative numbers and its errors (ZeroDivisionError, IndexError, KeyError, NameError, and TypeError for “a” + 1). Not supported yet: classes, for…of and for…in in JavaScript, switch, async, imports, slicing, tuples, keyword arguments, f-strings and template-string interpolation.
Background: Execution Visualizers and the Call Stack
The best-known tool of this kind is Online Python Tutor, described by Philip J. Guo in “Online Python Tutor: Embeddable Web-Based Program Visualization for CS Education”, Proceedings of the 44th ACM Technical Symposium on Computer Science Education (SIGCSE ’13, Denver, March 2013), pages 579–584 (doi:10.1145/2445196.2445368). Despite the name it now visualizes Python, Java, C, C++ and JavaScript, and its own site (pythontutor.com) reports that since 2010 more than 25 million people in more than 180 countries have used it on over 500 million pieces of code.
The design is the one this page uses: run the program once, record a trace (a list of steps, each with the event, the current line, the global variables and the stack of frames), then let the learner move through the recorded trace in either direction. Python Tutor’s own C and C++ article shows that trace format and explains that its C visualizer uses Valgrind to read memory safely while the program runs.
The call stack is what makes recursion visible. MDN describes it as the mechanism an interpreter uses to keep track of its place in a script that calls multiple functions: when a function is called it is added to the stack, and when it finishes it is taken off and execution resumes where it left off; if the stack grows past the space it was given, the result is a stack overflow (MDN: Call stack). Python’s language reference puts it in its own terms: each code block, such as a function body, is executed in an execution frame (Python execution model).
Scope is where the two languages differ most for a beginner. In JavaScript, let declarations are scoped to blocks as well as functions (MDN: let), so a loop counter vanishes when its loop ends. In Python, names in a for loop’s target list are not deleted when the loop finishes (Python reference: the for statement), so the counter is still there afterwards. The call-stack panel shows both.
About This Tool
The Code Execution Visualizer runs a short JavaScript or Python program and lets you replay it one step at a time: the current line, every variable in every frame of the call stack, the output so far, how deep the calls go over the whole run, and a plain-English note for each step. Thirteen examples cover recursion, loops, arrays, objects and dictionaries in both languages.
Unlike tools that send your code to a server to be traced, this one runs a small interpreter written for the Code Lab inside your browser. Nothing you type is uploaded, and it keeps working offline. The trade-off is honesty about scope: it runs a teaching subset, not the whole of either language, and it says so when it meets something it does not support. When your code touches JSON, regular expressions, hashing, colours or number bases, it points you to the Utilities Bunker tool for that job.
It is for people learning to program, teachers who want to show what the computer does on each line, and anyone checking a recursive function or an off-by-one loop by hand.
Related tools: Code Inspector, Coding Lessons, and Sorting Algorithm Visualizer.
Frequently Asked Questions
What code can it run?
A teaching subset of each language, run by the Code Lab’s own interpreter. JavaScript: let/const/var, numbers, strings, booleans, arrays and objects, all the usual operators including ** and %=, if/else, while and C-style for loops, function declarations, arrow functions, recursion, push/pop/map/filter/forEach/join/slice/includes, console.log and a small Math. Python: def and return, if/elif/else, while, for over range(), lists, strings and dicts, in and not in, print, len, range, str, int, float, sum, min and max. Not yet: classes, for…of, switch, async, imports, slicing, tuples, f-strings and template-string interpolation.
Why did it stop with “Stopped after 5000 steps”?
Every program is recorded step by step before you see it, so a loop that never ends would never finish recording. The engine stops at 5,000 steps and shows everything up to that point. A while (true) loop with no break hits the limit, and so does a big input to a slow algorithm: naive fib(14) fits in 4,880 steps, but fib(15) makes 1,973 calls and runs past 5,000. Try a smaller input.
Why is my loop counter gone after a JavaScript loop but still there in Python?
Because the two languages scope it differently, and the call stack panel shows it. In JavaScript, let is block-scoped, so the i of for (let i = 1; i <= 5; i++) disappears when the loop ends and console.log(i) afterwards fails with “i is not defined”. Python has no block scope: after for i in range(1, 6) the name i is still there, holding 5. The Python reference says names in a for loop’s target list are not deleted when the loop finishes.
Is this like Python Tutor?
Same idea, and Python Tutor is the best-known tool of its kind: record a trace of every step, then let you move through it. The difference is where the code runs. Here a small interpreter written for the Code Lab runs inside your browser, so nothing is sent anywhere and the page works offline, but it only covers the teaching subset above, while Python Tutor runs the real languages, including Java, C and C++.
Are the results exactly what real JavaScript or Python would do?
For the supported subset, yes for output and errors: Python’s -7 % 3 is 2 (JavaScript gives -1), “a” + 1 is a TypeError in Python, [1, null].join(“-”) is “1-” in JavaScript, and a dict prints as {'red': 3}. One known difference: Python’s 6 / 2 shows 3 here instead of 3.0, because the engine stores every number the same way.
How do I use the Code Execution Visualizer?
Just type your numbers. The answer shows up right away — there is no button to press. Change anything and it updates by itself.
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
See recursion unwind
factorial(5) stacks 5 frames, hits the base case at step 17, then returns 1, 2, 6, 24 and 120 on the way back down.
Find an off-by-one
Step a for loop and watch the final test: i < 5 stops after 4 passes and a total of 10, not the 15 you wanted.
Trace a sort by hand
Bubble sort on [5, 2, 8, 1, 9, 3] takes 76 steps; after the first outer pass the 9 has bubbled to the end.
Check a dictionary update
The word-count example shows counts['red'] going 1, 2, 3 as the loop meets “red” three times in five words.
Debug a lesson
From a Coding Lessons exercise, Visualize hands your code here and starts it at step 1.
Last updated: