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

              
                <h1>Hashmap instead of switch?</h1>
<p>Benefits of hashmaps over switch statements:</p>

<ul>
    <li>Easier to read (arguable)</li>
    <li>Pattern better matches other js (switch is an outlier)</li>
    <li>Portable</li>
    <li>more that don't come to mind atm...</li>
</ul>

Given:
<pre>
function choose(key) {
    const data = {
        abc: () => "Yep - key exists",
        def: () => data["abc"](), // duplicates abc
        default: () => "Nope - default!"
    };

    return data[data[key] ? key : "default"]();
}
</pre>

Result:
<div id="output"></div>
              
            
!

CSS

              
                body {
    margin: 25px;
}

#output, pre {
    outline: 1px dashed green;
    padding: 1rem;
}
.call, pre {
    color: green;
    font-family: monospace;
}

.result {
    color: orangered;
}

              
            
!

JS

              
                // returns the default if the key doesn't exist
function choose(key) {
    const data = {
        abc: () => "Yep - key exists",
        def: () => data["abc"](), // duplicates abc
        default: () => "Nope - default!"
    };

    return data[data[key] ? key : "default"]();
}

// OUTPUT - IGNORE BELOW...
const output = document.getElementById("output");
const content = (funcName, func) => (...args) => `
  <div>
    <span class="call">${funcName}(${args
    .map((arg) => (typeof arg === "string" ? `"${arg}"` : arg))
    .join(", ")}):</span>
    <span class="result">${func(...args)}</span>
  </div>
`;
const choice = content("choose", choose);
const print = (msg = "") => (output.innerHTML += choice(msg));

print("abc");
print("def");
print("foo");

              
            
!
999px

Console