Interactive JavaScript Sandbox

1.2.8 · active · verified Tue Apr 21

JavaScript Playgrounds is a library that provides an embeddable, interactive JavaScript sandbox environment, primarily designed for educational purposes and quick code experimentation. It allows users to execute JavaScript code directly within a web page, with support for various presets like React and React Native. The package, currently at version 1.2.8, does not specify a strict release cadence but appears to be actively maintained, being used in several 'Express' educational guides. Its key differentiators include easy integration as either a React component or a direct iframe, and the ability to pre-configure the sandbox with specific code and presets, making it highly versatile for showcasing or teaching JavaScript concepts without requiring complex setup for the end-user. Configuration parameters are passed via the URL hash, supporting both JSON and URI encoding for complex setups.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates embedding the `Playground` React component with initial code and a React preset.

import Playground from 'javascript-playgrounds';
import React from 'react';

// This component provides an interactive JavaScript sandbox.
// It defaults to a basic JavaScript environment.
// To configure with specific code or presets, pass props.
export default function App() {
  // Example: Setting initial code
  const initialCode = `
const message = 'Hello from the sandbox!';
console.log(message);

function greet(name) {
  return 'Hello, ' + name + '!';
}

console.log(greet('World'));
`;

  return (
    <div style={{ padding: '20px' }}>
      <h1>My Interactive Sandbox</h1>
      <Playground
        style={{ width: '100%', height: '600px', border: '1px solid #ddd' }}
        code={initialCode}
        preset="react" // Example: use the React preset
        // You can also specify a custom base URL if needed, e.g.,
        // baseURL="https://my-cdn.com/javascript-playgrounds"
      />
      <p>Experiment with JavaScript, React, or other presets directly in this embedded editor.</p>
    </div>
  );
}

view raw JSON →