A JavaScript playground example is a bounded snippet that runs in a dedicated browser Worker, where console messages, a final value, or an error can be reviewed before a 1,500-millisecond timeout stops unfinished work.
This example uses reduce to total an array and then verifies both console and result output. The JavaScript Playground evaluates short snippets locally in a fresh unique-origin sandbox, keeping the task focused on the code rather than page access or network requests.

A Complete JavaScript Playground Example
Paste the following snippet into the editor to test an array calculation, produce a console message, and return the final value separately.
const prices = [12, 8, 5];
const total = prices.reduce((sum, price) => sum + price, 0);
console.log("Total:", total);
total
The expected console message is Total: 25, and the expected result is 25. The calculation is 12 + 8 + 5 = 25.
Two output paths are used deliberately. The console.log call creates a console entry, while the final total expression is reported as the result when it can be formatted. This makes the example useful when you want to confirm both the value produced by a function and the value displayed by your own logging code.
Run the Example and Read Its Output
- Open the JavaScript Playground and paste the complete snippet into the editor.
- Confirm that the snippet is bounded and does not depend on DOM elements, browser windows, Node.js APIs, imported packages, or network resources. The editor accepts up to 30,000 characters.
- Select Run JavaScript. This creates a new isolated Worker and a fresh unique-origin sandbox for that execution.
- Review the console, result, error, or timeout output before changing the code. For this example, look for the console message and the separately reported result.
- Make one focused change, such as replacing an array value, and run the snippet again. Each rerun uses a fresh sandbox and replaces the prior one rather than carrying worker state forward.
Reviewing the current output before editing makes it easier to identify whether a change affected the result, introduced an error, or pushed asynchronous work beyond the time limit.
What the Example Demonstrates
- The prices array contains three numeric values that can be processed entirely in memory.
- The reduce callback adds each array value to an accumulator that starts at zero.
- The console.log call adds a labeled console entry containing the calculated total.
- The final expression exposes the same total through the playground's result output.
- The fresh run provides a controlled environment for comparing the snippet before and after an edit.
This pattern works well for arrays, strings, plain objects, algorithms, parsing, arithmetic, and language experiments. It does not turn the playground into a browser-page environment or a full Node.js runtime.
Choose the Right Output Signal
| Output | What it indicates | What to check next |
|---|---|---|
| Console message | The playground captured a console.log or console.error call. | Check the message text and compare it with the final result. |
| Result | The final expression produced a value that could be formatted for display. | Confirm that the returned value is the expression you intended to evaluate. |
| Error | The snippet failed, or it attempted to use behavior unavailable in the Worker. | Read the error, correct the code, and rerun it in a fresh sandbox. |
| Timeout | The Worker was terminated after 1,500 milliseconds before the work completed. | Look for a long synchronous loop or asynchronous work that did not settle in time. |
Values are formatted with JSON when possible and fall back to a string. Circular or unusual objects may therefore appear simplified. If you need to inspect a particular property, log that property explicitly rather than assuming the complete object will be displayed.
Adapt the Snippet for Common Tasks
| Task | Example pattern | Relevant boundary |
|---|---|---|
| Transform an array | Use map, filter, or reduce on literal or in-memory values. | The data must already exist in the snippet; the playground cannot read a page's DOM collections. |
| Edit a string | Use methods such as trim, split, replace, or toLowerCase. | Only the supplied string is available. |
| Transform an object | Build a plain object or use Object.fromEntries with in-memory entries. | Browser storage, cookies, and page session data are unavailable. |
| Parse JSON | Pass a string to JSON.parse and inspect the returned value or error. | Invalid input produces an error rather than a repaired result. |
| Test a Promise | Return a Promise chain from the final expression. | Promises are awaited until they settle, but unfinished work can still be stopped by the timeout. |
Keep each experiment focused enough to finish within the execution window. The timeout is a wall-clock guard rather than a performance benchmark, and browser scheduling can affect whether asynchronous work completes in time.
Limits That Can Change the Result
- The editor accepts up to 30,000 characters, so larger source files do not fit in one run.
- A worker that is still running after 1,500 milliseconds is terminated. This prevents an endless loop from freezing the parent page, but it can also cut off slow or unfinished asynchronous work.
- The Worker has no DOM, document, window, localStorage, cookies, or page session.
- Network connections are blocked by the sandbox's Content Security Policy. Package imports and a package runtime are also unavailable.
- The iframe allows scripts but denies same-origin access, forms, navigation, popups, and downloads. It cannot be used to interact with the surrounding page.
- The tool provides no persistent projects or files, package management, imports, Node.js APIs, TypeScript compilation, or debugging breakpoints.
- Every run creates a new cryptographically random channel token and replaces the prior sandbox. Output is accepted only when its source, fixed marker, and current token all match.
Execution occurs in a Worker rather than the user-interface thread, which explains why a runaway loop cannot lock up the parent page. MDN's Web Workers API guide provides more background on that browser execution model, while MDN's iframe sandbox reference explains the restrictions applied to embedded content.
Review Code Before Running It
The isolation is a defense boundary, not permission to execute unknown or malicious code. Read a snippet before submitting it, especially if it came from a message, an untrusted repository, or a generated response. Do not paste passwords, API keys, session tokens, private customer data, confidential information, or proprietary source code into any online editor.
Network blocking and a separate Worker reduce the risk of accidental access, but they do not make unreviewed code appropriate to run. Keep experiments small, remove sensitive values, and stop using the playground if your task requires capabilities its sandbox does not provide.
Move Production Work to a Local Project
The playground stores no projects and provides no persistent files, so it is best treated as a scratchpad for short experiments. When code needs dependencies, a DOM, a Node.js API, repeatable tests, or deployment configuration, move it to a version-controlled local project. Add tests, linting, dependency review, and run the code in the same runtime that will execute it in production.
That separation prevents a convenient browser experiment from being mistaken for a complete application environment. Use the playground to clarify a calculation, inspect an algorithm, or reproduce an error, then reproduce the confirmed logic in a properly configured local project.