Format Code Now
/
Web & markupJavaScript Playground Next: JavaScript Tester ›

JavaScript Playground

WM-137

Type JavaScript and press Run. Console output appears on the right, and anything your code adds to the page shows in the frame below it, all inside a sandbox that cannot touch this site.

Runs in your browser. Nothing is uploaded.

JavaScript

Page HTML (optional)

Ctrl+Enter (Cmd+Enter on a Mac) runs the code.

Console

  1. Press Run. console.log output appears here.

Page

How to use it

  1. Type or paste JavaScript in the editor, or press Sample for a short example.
  2. Optionally open Page HTML and add markup your code can select, such as a list or a button.
  3. Press Run, or Ctrl+Enter (Cmd+Enter on a Mac).
  4. Read console.log output and errors in the console pane, and see the page your code changed in the frame.
  5. Press Stop to end timers or a slow run, then edit and run again.

What runs where

Each run builds a fresh document inside an iframe with sandbox set to allow-scripts and nothing more. That frame gets its own blank origin, so your code cannot read this page, its cookies or its storage, and a content security policy blocks fetch, WebSocket and images from other sites. console.log, info, warn, error, debug, dir and table are copied to the console pane as text. Uncaught errors and rejected promises show there too, with the line number in your code when the browser reports one.

Stopping runaway code

Before your code runs, every for, while and do-while condition gets a small check added. If one loop keeps the page busy longer than the limit you pick (2 or 10 seconds), it throws an error instead of freezing the tab. for-of and for-in loops, deep recursion and long single calls are not covered. Stop goes further: it throws the whole frame away, which also ends setTimeout and setInterval callbacks that would otherwise keep firing.

Limits of the sandbox

Because the frame has no origin of its own, localStorage and cookies throw errors inside it, and import statements do not work. alert() is printed to the console instead of opening a box; confirm and prompt return nothing. Pop-ups, form posts and network requests are blocked. Your last code is kept in this browser so it is still there after a refresh, and Share link puts short code into the link after the # sign.

Error messages and what to do

MessageWhat it meansFix
Type some JavaScript first, or press Sample.Run was pressed with an empty editor.Type some code, or press Sample to load an example.
A loop ran for more than 2 seconds and was stopped. Pick a higher loop limit if it is meant to run that long.A for, while or do loop kept running past the limit, so the loop check threw this error inside the sandbox.Look for a loop whose condition never becomes false. If the work is meant to be long, pick 10 seconds or Never.

Questions people ask

How do I run JavaScript online?

Paste your code into the editor and press Run or Ctrl+Enter. The code runs in a sandboxed frame in your own browser, and anything passed to console.log appears in the console pane on the right, with errors shown in red.

Can I use the DOM in this JavaScript playground?

Yes. The sandbox has a real document, so document.querySelector, createElement and event listeners work. Add markup in the Page HTML box and it is placed in the body before your code runs. The frame under the console shows the result.

What happens if my code has an infinite loop?

A for, while or do loop that keeps the page busy past the limit you pick is stopped with an error. Recursion and long single calls are not checked; if the run seems stuck, press Stop, which throws the sandbox away and ends everything inside it.

Why does localStorage throw an error here?

The sandbox frame is not given an origin, which is what keeps your code away from this site and your data. Without an origin the browser refuses storage and cookies. To try storage code, use a plain variable as a stand-in or run it in a local page.

Can I import npm packages?

No. Module imports and network requests are blocked inside the sandbox, so nothing is downloaded. Paste the function you need into the editor instead. Plain modern JavaScript such as async functions, classes and optional chaining works as it does in your browser.

Is my code sent to a server?

No. The code is placed into a frame on this page and run by your browser. It is kept in this browser so it survives a refresh, and Share link stores it after the # in the address, which browsers do not send to servers.

About this tool

Your code runs in a sandboxed frame with scripts allowed and nothing else: it gets its own blank origin, so it cannot read this page, your cookies or your storage, and a content security policy blocks fetch, images from other sites and every other network request. console.log, info, warn, error, table and uncaught errors are copied to the console pane as text, with the line number for errors. Code is checked so that a for, while or do loop that keeps the page busy past the limit you pick is stopped with an error; recursion and other long work are not limited, and Stop ends everything by throwing the frame away. localStorage, alert prompts, pop-ups and modules (import) are not available inside the sandbox; alert text is printed to the console instead.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.