About the JavaScript compiler
How to use the JavaScript compiler
Write a program in the editor or pick a template: input and arithmetic, map, filter and reduce, a prime check, strings, a word count with a Map, a class, recursion, try and catch, sorting objects, and promises with setTimeout. Press Run or Ctrl+Enter. console.log, console.info and console.table print to the output; console.error and console.warn print in red.
How input works
Each prompt() returns the next line of the Input tab, as text; use Number() or parseInt() for a number. When no line is left, prompt() returns null, as a browser does when the user cancels, and the compiler notes that the program wanted more input.
What runs your code
Your code runs as real JavaScript in your browser's own engine, inside a sandbox with no access to the page, the network or your files: there is no document, window or fetch. setTimeout, setInterval, promises and async functions work, with timers that run on a virtual clock, so a program that waits 10 seconds finishes at once. Math.random gives the same numbers every run, so tests stay repeatable.
Each run may take 5 seconds; then it is stopped. Output is kept up to 10,000 lines or 1 MB, and the rest is cut with a notice. Programs cannot reach the network, and no packages or libraries can be installed: what a language brings with it is what you can use. A loop that never ends is stopped at 5 seconds, keeping what it printed.
See it step by step
Visualize this run opens the program and its input in the JavaScript visualizer, which shows the call stack, variables, and arrays, objects, Maps and class instances in the heap, with arrows for references. You can watch the event loop pick up timers and promises. It follows the first 1,000 steps.
Questions
- Is this Node.js?
- No. It is your browser's JavaScript engine in a sandbox. There is no require(), no file system and no network, but the language itself is the same.
- How do I read input?
- Call prompt() once per line of the Input tab. It returns text, so convert numbers with Number().
- Do setTimeout and async/await work?
- Yes, on a virtual clock: the order of events is real, but waiting takes no time.
- Can I use document or alert()?
- No. The code runs without a web page. Use console.log for output.
See also
- The JavaScript code visualizer: run code one step at a time and see its memory.
- JavaScript: Programming for the Web: the course, with lessons and examples you can open here.
- Practice problems, checked against hidden tests.
- Online Python compiler
- Online C compiler
- Online C++ compiler
- Online Java compiler
- Online SQL compiler
- Online 8086 Assembly compiler