JavaScript Playground
Run and test JavaScript code directly in your browser with console output.
このツールについて
A lightweight JavaScript REPL and playground that lets you write, run, and experiment with JavaScript code directly in the browser. Code runs in an isolated context using a sandboxed iframe so it cannot affect the page. Console.log output, errors, and return values are all displayed in a clean output panel. Choose from pre-loaded examples covering Hello World, array methods, and async patterns. Clear the console, copy your code, and iterate quickly without any setup.
使い方
- 1 Type or paste JavaScript code into the editor panel.
- 2 Click 'Run' or press Ctrl+Enter to execute the code.
- 3 View console output, return values, and any errors in the output panel.
- 4 Click an example button to load a pre-built code snippet.
- 5 Click 'Clear' to reset the output panel, or 'Copy' to copy your code.
What a browser JavaScript playground gives you
A playground is a write-run-see loop with zero setup: you type JavaScript, press Run, and read the console output beside the editor — no Node.js install, no project folder, no build step. That makes it ideal for testing a snippet, checking how an array method behaves, prototyping a small algorithm, or learning the language by experiment. This playground runs your code inside an isolated sandboxed iframe, so the code you write cannot touch the page around it, read its cookies, or interfere with other tabs. Your code is never sent to a server; it executes locally in your own browser.
How the sandbox actually works
When you click Run, the tool creates a hidden <iframe> with the attribute sandbox="allow-scripts". That single restriction is doing a lot of work: allow-scripts lets your code run, but the absence of allow-same-origin means the iframe is treated as a different origin, cut off from the parent document's DOM, storage, and cookies. Your code is wrapped in its own function scope and given a custom console object whose log, warn, error, and info methods collect their arguments into an array. When execution finishes, the iframe posts that array back to the page with postMessage, the tool renders it, and the iframe is removed. A 5-second timeout guards against infinite loops so a runaway while(true) cannot freeze the page.
Reading the output panel
The output is color-coded by source so you can scan it quickly:
| Output type | Comes from | Shown as |
|---|---|---|
| Log | console.log(...) | Plain text |
| Warning | console.warn(...) | Yellow, prefixed ⚠ |
| Error | console.error(...) or a thrown exception | Red, prefixed ✗ |
| Info | console.info(...) | Blue, prefixed ℹ |
| Return value | The final expression's value, if not undefined | Green italic, prefixed → |
Objects passed to console.log are serialized with JSON.stringify and pretty-printed, so logging { id: 1, name: "Alice" } shows a readable, indented object rather than [object Object]. A status line below the panel reports how long execution took and how many output lines were produced.
A worked example: async code and timing
Load the Async/Await example and run it. It defines a delay helper that returns a promise resolving after a set time, then awaits a simulated fetch. You will see "Fetching user 42..." appear, followed after a pause by the resolved user object. This works because the sandbox waits a short moment after your synchronous code finishes before collecting logs, giving promises and timers a chance to resolve and report. It is a good demonstration of why await matters: comment out the await and you would see the function return a pending promise instead of the data.
Built-in examples worth studying
- Hello World covers template literals and
new Date()— the basics of producing output. - Array Methods chains
map,filter, andreduce, the three functions you will reach for most when transforming data. - Async/Await shows promises,
Promise.allfor parallel work, andtry/catcherror handling. - Fibonacci demonstrates recursion with memoization — caching results so each value is computed once instead of exponentially many times.
Practical tips
- Press Ctrl+Enter (or Cmd+Enter) to run. It is faster than reaching for the Run button when you are iterating.
- Use
console.loggenerously. The panel only shows what you log or return, so sprinkle logs to inspect intermediate values while debugging. - Return a value to see it highlighted. The last expression's value is shown in green, which is handy for checking the result of a calculation without an extra log.
- Tab inserts two spaces. The editor traps the Tab key for indentation instead of moving focus, so you can format code naturally.
Common mistakes and limitations
- Expecting DOM access. The sandbox has no access to this page's DOM by design. Code that calls
document.getElementByIdon the parent will not find your elements — that isolation is the security feature. - Long-running loops time out. Anything past 5 seconds is cut off and reported as a timeout. Optimize tight loops or reduce iteration counts when testing.
- Async logs arriving late. Output from a timer longer than the sandbox's collection window may be missed. Keep simulated delays short in the playground.
- Assuming it persists. Your code is not saved between sessions. Copy anything you want to keep with the Copy button before you close the tab.
When to graduate to a real environment
The playground is perfect for snippets, learning, and quick checks. For anything with multiple files, npm packages, a test suite, or filesystem access, move to Node.js or a full editor. But for "does this method do what I think?" — the question that interrupts a hundred coding sessions a day — running it here is faster than any other answer.