Lab · Playground
A JavaScript playground
Edit the code and run it in your browser — a quick demonstration of the site's playgrounds.
Code blocks marked as playgrounds become small editors. Change the code and press Run (or Cmd/Ctrl + Enter). JavaScript runs in a Web Worker (so Stop can end a runaway loop); Python runs on Pyodide; HTML renders in place. Other playgrounds run C and C++, Lua, Ruby, SQL, Prolog and Scheme, BASIC, and Rust and Go — or try anything in the language sandbox.
Fibonacci, the slow way and the fast way
const slow = n => n < 2 ? n : slow(n - 1) + slow(n - 2);
const fast = n => { let [a, b] = [0, 1]; for (let i = 0; i < n; i++) [a, b] = [b, a + b]; return a; };
console.log('slow(30) =', slow(30));
console.log('fast(90) =', fast(90));
The same idea in Python
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
print([fib(n) for n in range(15)])
A bouncing ball in HTML
<canvas id="c" width="480" height="240" style="background:#111;border-radius:8px"></canvas>
<script>
const c = document.getElementById('c'), g = c.getContext('2d');
let x = 60, y = 60, vx = 3, vy = 0;
(function tick() {
vy += 0.4; x += vx; y += vy;
if (y > 210) { y = 210; vy = -vy * 0.92; }
if (x < 30 || x > 450) vx = -vx;
g.clearRect(0, 0, 480, 240);
g.fillStyle = '#e8874f'; g.beginPath(); g.arc(x, y, 30, 0, 7); g.fill();
requestAnimationFrame(tick);
})();
</script>