Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="card">
  <h2>Array Methods Playground</h2>
  <pre id="output">/* See results here */</pre>
  
  <textarea id="array-input" placeholder="Enter array, e.g. [1,2,3]" rows="2"></textarea>
  <input type="text" id="method-input" placeholder="Method & callback, e.g. map(x=>x*2)" />
  <button id="run-btn">Run Method</button>
</div>

              
            
!

CSS

              
                body {
  font-family: sans-serif;
  background: #f7f7f7;
  display: flex;
  justify-content: center;
  align-items: start;
  padding: 2rem;
  margin: 0;
}

.card {
  background: white;
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  width: 400px;
}

textarea, input {
  width: 100%;
  padding: 0.5rem;
  margin: 0.5rem 0;
  box-sizing: border-box;
}

button {
  padding: 0.6rem 1.2rem;
  border: none;
  background: #333;
  color: white;
  border-radius: 5px;
  cursor: pointer;
}

pre {
  background: #eee;
  padding: 1rem;
  border-radius: 5px;
  min-height: 60px;
}

              
            
!

JS

              
                const output = document.getElementById('output');
const arrInput = document.getElementById('array-input');
const methodInput = document.getElementById('method-input');
const runBtn = document.getElementById('run-btn');

// List of known mutating methods (they change the original array)
const mutatingMethods = ['push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse'];

runBtn.addEventListener('click', () => {
  let arr;
  try {
    arr = JSON.parse(arrInput.value);
    if (!Array.isArray(arr)) throw new Error();
  } catch {
    output.textContent = 'Invalid array! Use something like [1, 2, 3]';
    return;
  }

  const expr = methodInput.value.trim();
  if (!expr) {
    output.textContent = 'Enter a method to run, like map(x => x * 2)';
    return;
  }

  const methodName = expr.split('(')[0].split('.')[0];

  try {
    let result;
    if (mutatingMethods.includes(methodName)) {
      // Use actual array so we can mutate it
      const copy = [...arr];
      const fn = new Function('arr', `arr.${expr}; return arr`);
      result = fn(copy);
    } else {
      // Non-mutating methods
      const fn = new Function('arr', `return arr.${expr}`);
      result = fn([...arr]);
    }

    output.textContent =
      `Original: ${JSON.stringify(arr)} \n\nAfter .${expr}: ${JSON.stringify(result)}`;
  } catch (e) {
    output.textContent = `❌ Error: ${e.message}`;
  }
});

              
            
!
999px

Console