JavaScript · Playground

JavaScript playground

Runs in an isolated Web Worker on your device: no DOM, no network, no eval. Read input with readline() / readAll(); print with console.log. A program is stopped after 5 seconds.

// Notice board for the pier ferry
console.log("Pier Ferry");
console.log("First sailing at 6:40");
const crossings = 14;
console.log("Crossings today:", crossings);
console.log("Fare:", 3.5, "per adult");
/* The line below is a comment,
   so it prints nothing. */
// console.log("Cancelled");

Output

Press Run to see the program’s output here.

Runs in an isolated Web Worker on your device: no DOM, no network, no eval. Read input with readline() / readAll(); print with console.log. A program is stopped after 5 seconds.

JavaScript examples

Each one was run with Node.js 22 before it was published. Load one, change it, run it.

  •  JavaScript · JavaScript Syntax and Your First Program: A program of console.log calls
    // Notice board for the pier ferry
    console.log("Pier Ferry");
    console.log("First sailing at 6:40");
    const crossings = 14;
    console.log("Crossings today:", crossings);
    console.log("Fare:", 3.5, "per adult");
    /* The line below is a comment,
       so it prints nothing. */
    // console.log("Cancelled");
  •  JavaScript · JavaScript Syntax and Your First Program: Blocks, braces and semicolons
    const seats = 2;
    if (seats > 0) {
      console.log("Seats available");
      console.log("Remaining:", seats);
    }
    console.log("Booking check done");
    const a = 1; const b = 2; console.log(a + b);
  •  JavaScript · Variables in JavaScript: let, const and var: Declaring, reassigning and reading an unassigned variable
    let tickets = 3;
    console.log("Tickets:", tickets);
    tickets = tickets + 2;
    console.log("After buying two more:", tickets);
    
    const venue = "Old Mill Theatre";
    console.log("Venue:", venue);
    
    let seat;
    console.log("Seat before assignment:", seat);
    seat = "B12";
    console.log("Seat after assignment:", seat);
  •  JavaScript · Variables in JavaScript: let, const and var: Block scope: let stays inside its braces, var leaks out
    let area = "lobby";
    if (true) {
      let area = "stage";
      var announced = "yes";
      console.log("Inside the block:", area);
    }
    console.log("After the block:", area);
    console.log("var leaked out:", announced);
    
    for (let i = 0; i < 3; i++) {
      // i exists only inside this loop
    }
    console.log("typeof i after the loop:", typeof i);
  •  JavaScript · Variables in JavaScript: let, const and var: const protects the binding, not the contents
    const order = { item: "espresso", qty: 1 };
    order.qty = 2;
    console.log(order);
    
    const prices = [2.5, 3];
    prices.push(4.25);
    console.log(prices);
    
    try {
      order = { item: "latte", qty: 1 };
    } catch (err) {
      console.log("Error:", err.message);
    }
    console.log(order.item);

How this page was checked. Every program on it was run with Node.js 22 at build time; runs in your browser in an isolated Web Worker by the publishing checks, and the output shown is what it printed. The Run buttons execute the same code on your device; nothing you type is sent anywhere.