elefcode
← All guides

Dev Basics · 4 min read

Running Python and JavaScript in the Browser

Online code runners used to send your code to a server. Modern ones run it entirely in your browser — JavaScript natively, and languages like Python via WebAssembly. That makes them instant, private, and free to run.

Try it yourself with the related tool.

Run code in the browser →

Advertisement

JavaScript and TypeScript

The browser already runs JavaScript, so a code runner only needs to execute it somewhere isolated and capture console.log output. TypeScript is first compiled to JavaScript in the page. Running the code in a Web Worker keeps an infinite loop from freezing the page, because the worker can be terminated.

Python via WebAssembly

Projects such as Pyodide compile the real CPython interpreter to WebAssembly. The first run downloads the interpreter — several megabytes — and later runs are fast. Pure-Python packages and many scientific ones like NumPy are available.

The limits

Browser code runs in a sandbox: there is no real file system beyond a virtual one, no arbitrary network sockets, no subprocesses, and network requests are subject to CORS. Packages that depend on native C extensions not built for WebAssembly will not install. For those, you still need a local environment.

Why it is safe

The same sandbox that limits the code protects you: code cannot read your files or reach other tabs. That makes in-browser runners a good way to try a snippet from a tutorial without installing anything.

Quick reference

Related guides